Bug Summary

File:root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h
Warning:line 1363, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name DominatorTree.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/devtools/shared/heapsnapshot -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/devtools/shared/heapsnapshot -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -D _GLIBCXX_ASSERTIONS=1 -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D GOOGLE_PROTOBUF_NO_RTTI -D MOZ_HAS_MOZGLUE -D MOZILLA_INTERNAL_API -D IMPL_LIBXUL -D MOZ_SUPPORT_LEAKCHECKING -D STATIC_EXPORTABLE_JS_API -I /root/firefox-clang/devtools/shared/heapsnapshot -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/devtools/shared/heapsnapshot -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/ipc/ipdl/_ipdlheaders -I /root/firefox-clang/ipc/chromium/src -I /root/firefox-clang/third_party/abseil-cpp -I /root/firefox-clang/toolkit/components/telemetry -I /root/firefox-clang/xpcom/base -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/x86_64-linux-gnu/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/backward -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-character-conversion -std=gnu++20 -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-09-26-101540-2480413-1 -x c++ /root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp

/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp

→
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#include "mozilla/devtools/DominatorTree.h"
6#include "mozilla/dom/DominatorTreeBinding.h"
7#include "mozilla/ErrorResult.h"
8
9namespace mozilla {
10namespace devtools {
11
12dom::Nullable<uint64_t> DominatorTree::GetRetainedSize(uint64_t aNodeId,
13 ErrorResult& aRv) {
14 JS::ubi::Node::Id id(aNodeId);
15 auto node = mHeapSnapshot->getNodeById(id);
16 if (node.isNothing()) return dom::Nullable<uint64_t>();
17
18 auto mallocSizeOf = GetCurrentThreadDebuggerMallocSizeOf();
19 JS::ubi::Node::Size size = 0;
20 if (!mDominatorTree.getRetainedSize(*node, mallocSizeOf, size)) {
21 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
22 return dom::Nullable<uint64_t>();
23 }
24
25 MOZ_ASSERT(size != 0,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(size != 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(size != 0))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("size != 0" " (" "The node should not have been unknown since we got it from the "
"heap snapshot." ")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 27); AnnotateMozCrashReason("MOZ_ASSERT" "(" "size != 0" ") ("
"The node should not have been unknown since we got it from the "
"heap snapshot." ")"); do { MOZ_CrashSequence(__null, 27); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
26 "The node should not have been unknown since we got it from the "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(size != 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(size != 0))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("size != 0" " (" "The node should not have been unknown since we got it from the "
"heap snapshot." ")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 27); AnnotateMozCrashReason("MOZ_ASSERT" "(" "size != 0" ") ("
"The node should not have been unknown since we got it from the "
"heap snapshot." ")"); do { MOZ_CrashSequence(__null, 27); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
27 "heap snapshot.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(size != 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(size != 0))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("size != 0" " (" "The node should not have been unknown since we got it from the "
"heap snapshot." ")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 27); AnnotateMozCrashReason("MOZ_ASSERT" "(" "size != 0" ") ("
"The node should not have been unknown since we got it from the "
"heap snapshot." ")"); do { MOZ_CrashSequence(__null, 27); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
;
28 return dom::Nullable<uint64_t>(size);
29}
30
31struct NodeAndRetainedSize {
32 JS::ubi::Node mNode;
33 JS::ubi::Node::Size mSize;
34
35 NodeAndRetainedSize(const JS::ubi::Node& aNode, JS::ubi::Node::Size aSize)
36 : mNode(aNode), mSize(aSize) {}
37
38 struct Comparator {
39 static bool Equals(const NodeAndRetainedSize& aLhs,
40 const NodeAndRetainedSize& aRhs) {
41 return aLhs.mSize == aRhs.mSize;
42 }
43
44 static bool LessThan(const NodeAndRetainedSize& aLhs,
45 const NodeAndRetainedSize& aRhs) {
46 // Use > because we want to sort from greatest to least retained size.
47 return aLhs.mSize > aRhs.mSize;
48 }
49 };
50};
51
52void DominatorTree::GetImmediatelyDominated(
53 uint64_t aNodeId, dom::Nullable<nsTArray<uint64_t>>& aOutResult,
54 ErrorResult& aRv) {
55 MOZ_ASSERT(aOutResult.IsNull())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aOutResult.IsNull())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aOutResult.IsNull()))), 0)))
{ do { } while (false); MOZ_ReportAssertionFailure("aOutResult.IsNull()"
, "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 55); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aOutResult.IsNull()"
")"); do { MOZ_CrashSequence(__null, 55); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
56
57 JS::ubi::Node::Id id(aNodeId);
58 Maybe<JS::ubi::Node> node = mHeapSnapshot->getNodeById(id);
59 if (node.isNothing()) return;
60
61 // Get all immediately dominated nodes and their retained sizes.
62 MallocSizeOf mallocSizeOf = GetCurrentThreadDebuggerMallocSizeOf();
63 Maybe<JS::ubi::DominatorTree::DominatedSetRange> range =
64 mDominatorTree.getDominatedSet(*node);
65 MOZ_ASSERT(do { static_assert( mozilla::detail::AssertionConditionType<
decltype(range.isSome())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(range.isSome()))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("range.isSome()"
" (" "The node should be known, since we got it from the heap snapshot."
")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 67); AnnotateMozCrashReason("MOZ_ASSERT" "(" "range.isSome()"
") (" "The node should be known, since we got it from the heap snapshot."
")"); do { MOZ_CrashSequence(__null, 67); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
66 range.isSome(),do { static_assert( mozilla::detail::AssertionConditionType<
decltype(range.isSome())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(range.isSome()))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("range.isSome()"
" (" "The node should be known, since we got it from the heap snapshot."
")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 67); AnnotateMozCrashReason("MOZ_ASSERT" "(" "range.isSome()"
") (" "The node should be known, since we got it from the heap snapshot."
")"); do { MOZ_CrashSequence(__null, 67); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
67 "The node should be known, since we got it from the heap snapshot.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(range.isSome())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(range.isSome()))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("range.isSome()"
" (" "The node should be known, since we got it from the heap snapshot."
")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 67); AnnotateMozCrashReason("MOZ_ASSERT" "(" "range.isSome()"
") (" "The node should be known, since we got it from the heap snapshot."
")"); do { MOZ_CrashSequence(__null, 67); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
68 size_t length = range->length();
69 nsTArray<NodeAndRetainedSize> dominatedNodes(length);
70 for (const JS::ubi::Node& dominatedNode : *range) {
71 JS::ubi::Node::Size retainedSize = 0;
72 if (NS_WARN_IF(!mDominatorTree.getRetainedSize(dominatedNode, mallocSizeOf,NS_warn_if_impl(!mDominatorTree.getRetainedSize(dominatedNode
, mallocSizeOf, retainedSize), "!mDominatorTree.getRetainedSize(dominatedNode, mallocSizeOf, retainedSize)"
, "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 73)
73 retainedSize))NS_warn_if_impl(!mDominatorTree.getRetainedSize(dominatedNode
, mallocSizeOf, retainedSize), "!mDominatorTree.getRetainedSize(dominatedNode, mallocSizeOf, retainedSize)"
, "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 73)
) {
74 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
75 return;
76 }
77 MOZ_ASSERT(retainedSize != 0,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(retainedSize != 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(retainedSize != 0))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("retainedSize != 0"
" (" "retainedSize should not be zero since we know the node is in "
"the dominator tree." ")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 79); AnnotateMozCrashReason("MOZ_ASSERT" "(" "retainedSize != 0"
") (" "retainedSize should not be zero since we know the node is in "
"the dominator tree." ")"); do { MOZ_CrashSequence(__null, 79
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
78 "retainedSize should not be zero since we know the node is in "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(retainedSize != 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(retainedSize != 0))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("retainedSize != 0"
" (" "retainedSize should not be zero since we know the node is in "
"the dominator tree." ")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 79); AnnotateMozCrashReason("MOZ_ASSERT" "(" "retainedSize != 0"
") (" "retainedSize should not be zero since we know the node is in "
"the dominator tree." ")"); do { MOZ_CrashSequence(__null, 79
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
79 "the dominator tree.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(retainedSize != 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(retainedSize != 0))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("retainedSize != 0"
" (" "retainedSize should not be zero since we know the node is in "
"the dominator tree." ")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 79); AnnotateMozCrashReason("MOZ_ASSERT" "(" "retainedSize != 0"
") (" "retainedSize should not be zero since we know the node is in "
"the dominator tree." ")"); do { MOZ_CrashSequence(__null, 79
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
;
80
81 dominatedNodes.AppendElement(
82 NodeAndRetainedSize(dominatedNode, retainedSize));
83 }
84
85 // Sort them by retained size.
86 NodeAndRetainedSize::Comparator comparator;
87 dominatedNodes.Sort(comparator);
88
89 // Fill the result with the nodes' ids.
90 JS::ubi::Node root = mDominatorTree.root();
91 aOutResult.SetValue(nsTArray<uint64_t>(length));
92 for (const NodeAndRetainedSize& entry : dominatedNodes) {
93 // The root dominates itself, but we don't want to expose that to JS.
94 if (entry.mNode == root) continue;
95
96 aOutResult.Value().AppendElement(entry.mNode.identifier());
97 }
98}
99
100dom::Nullable<uint64_t> DominatorTree::GetImmediateDominator(
101 uint64_t aNodeId) const {
102 JS::ubi::Node::Id id(aNodeId);
103 Maybe<JS::ubi::Node> node = mHeapSnapshot->getNodeById(id);
104 if (node.isNothing()) return dom::Nullable<uint64_t>();
1
Assuming the condition is false
2
Taking false branch
105
106 JS::ubi::Node dominator = mDominatorTree.getImmediateDominator(*node);
3
Calling 'DominatorTree::getImmediateDominator'
107 if (!dominator || dominator == *node) return dom::Nullable<uint64_t>();
108
109 return dom::Nullable<uint64_t>(dominator.identifier());
110}
111
112/*** Cycle Collection Boilerplate
113 * *****************************************************************/
114
115NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DominatorTree, mParent, mHeapSnapshot)static_assert(std::is_base_of_v<nsWrapperCache, DominatorTree
>, "Class should inherit nsWrapperCache"); DominatorTree::
cycleCollection DominatorTree::_cycleCollectorGlobal( nsCycleCollectionParticipant
::FlagMaybeSingleZoneJSHolder); void DominatorTree::cycleCollection
::Trace( void* p, const TraceCallbacks& aCallbacks, void*
aClosure) { DominatorTree* tmp = DowncastCCParticipant<DominatorTree
>(p); TraceWrapper(p, aCallbacks, aClosure); (void)tmp; } void
DominatorTree::cycleCollection::TraceWrapper( void* p, const
TraceCallbacks& aCallbacks, void* aClosure) { DominatorTree
* tmp = DowncastCCParticipant<DominatorTree>(p); tmp->
TraceWrapper(aCallbacks, aClosure); } void DominatorTree::cycleCollection
::Unlink(void* p) { DominatorTree* tmp = DowncastCCParticipant
<DominatorTree>(p); ImplCycleCollectionUnlink(tmp->mParent
); ImplCycleCollectionUnlink(tmp->mHeapSnapshot); tmp->
ReleaseWrapper(p); (void)tmp; } nsresult DominatorTree::cycleCollection
::TraverseNative( void* p, nsCycleCollectionTraversalCallback
& cb) { DominatorTree* tmp = DowncastCCParticipant<DominatorTree
>(p); cb.DescribeRefCountedNode(tmp->mRefCnt.get(), "DominatorTree"
); ImplCycleCollectionTraverse(cb, tmp->mParent, "mParent"
, 0); ImplCycleCollectionTraverse(cb, tmp->mHeapSnapshot, "mHeapSnapshot"
, 0); (void)tmp; return NS_OK; }
116
117NS_IMPL_CYCLE_COLLECTING_ADDREF(DominatorTree)MozExternalRefCountType DominatorTree::AddRef(void) { static_assert
(!std::is_destructible_v<DominatorTree>, "Reference-counted class "
"DominatorTree" " should not have a public destructor. " "Make this class's destructor non-public"
); do { static_assert( mozilla::detail::AssertionConditionType
<decltype(int32_t(mRefCnt) >= 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0"
" (" "illegal refcnt" ")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 117); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0"
") (" "illegal refcnt" ")"); do { MOZ_CrashSequence(__null, 117
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false); _mOwningThread.AssertOwnership("DominatorTree" " not thread-safe"
); nsISupports* base = DominatorTree::cycleCollection::Upcast
(this); nsrefcnt count = mRefCnt.incr(base); NS_LogAddRef((this
), (count), ("DominatorTree"), (uint32_t)(sizeof(*this))); return
count; }
118NS_IMPL_CYCLE_COLLECTING_RELEASE(DominatorTree)MozExternalRefCountType DominatorTree::Release(void) { do { static_assert
( mozilla::detail::AssertionConditionType<decltype(int32_t
(mRefCnt) > 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) > 0))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) > 0"
" (" "dup release" ")", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 118); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) > 0"
") (" "dup release" ")"); do { MOZ_CrashSequence(__null, 118
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false); _mOwningThread.AssertOwnership("DominatorTree" " not thread-safe"
); nsISupports* base = DominatorTree::cycleCollection::Upcast
(this); nsrefcnt count = mRefCnt.decr(base); if (count == 0) {
NS_CycleCollectableHasRefCntZero(); } NS_LogRelease((this), (
count), ("DominatorTree")); return count; } void DominatorTree
::DeleteCycleCollectable(void) { delete (this); }
119
120NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DominatorTree)nsresult DominatorTree::QueryInterface(const nsIID& aIID,
void** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak
(NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!"
, "aInstancePtr", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 120); MOZ_PretendNoReturn(); } } while (0); nsISupports* foundInterface
; if (TopThreeWordsEquals( aIID, (nsXPCOMCycleCollectionParticipant
::kIID), (nsCycleCollectionISupports::kIID)) && (LowWordEquals
(aIID, (nsXPCOMCycleCollectionParticipant::kIID)) || LowWordEquals
(aIID, (nsCycleCollectionISupports::kIID)))) { if (LowWordEquals
(aIID, (nsXPCOMCycleCollectionParticipant::kIID))) { *aInstancePtr
= DominatorTree::cycleCollection::GetParticipant(); return NS_OK
; } if (LowWordEquals(aIID, (nsCycleCollectionISupports::kIID
))) { *aInstancePtr = DominatorTree::cycleCollection::Upcast(
this); return NS_OK; } foundInterface = nullptr; } else
121 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRYif (aIID.Equals((nsWrapperCache::kIID))) { *aInstancePtr = static_cast
<nsWrapperCache*>(this); return NS_OK; } else
122 NS_INTERFACE_MAP_ENTRY(nsISupports)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsISupports>)) foundInterface = static_cast
<nsISupports*>(this); else
123NS_INTERFACE_MAP_ENDfoundInterface = 0; nsresult status; if (!foundInterface) { do
{ static_assert( mozilla::detail::AssertionConditionType<
decltype(!aIID.Equals((nsISupports::kIID)))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::kIID
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!aIID.Equals((nsISupports::kIID))", "/root/firefox-clang/devtools/shared/heapsnapshot/DominatorTree.cpp"
, 123); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::kIID))"
")"); do { MOZ_CrashSequence(__null, 123); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE
; } else { (foundInterface)->AddRef(); status = NS_OK; } *
aInstancePtr = foundInterface; return status; }
124
125/* virtual */
126JSObject* DominatorTree::WrapObject(JSContext* aCx,
127 JS::Handle<JSObject*> aGivenProto) {
128 return dom::DominatorTree_Binding::Wrap(aCx, this, aGivenProto);
129}
130
131} // namespace devtools
132} // namespace mozilla

←

/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h

→
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#ifndef js_UbiNodeDominatorTree_h
6#define js_UbiNodeDominatorTree_h
7
8#include "mozilla/Maybe.h"
9#include "mozilla/UniquePtr.h"
10
11#include <utility>
12
13#include "js/AllocPolicy.h"
14#include "js/UbiNode.h"
15#include "js/UbiNodePostOrder.h"
16#include "js/Utility.h"
17#include "js/Vector.h"
18
19namespace JS {
20namespace ubi {
21
22/**
23 * In a directed graph with a root node `R`, a node `A` is said to "dominate" a
24 * node `B` iff every path from `R` to `B` contains `A`. A node `A` is said to
25 * be the "immediate dominator" of a node `B` iff it dominates `B`, is not `B`
26 * itself, and does not dominate any other nodes which also dominate `B` in
27 * turn.
28 *
29 * If we take every node from a graph `G` and create a new graph `T` with edges
30 * to each node from its immediate dominator, then `T` is a tree (each node has
31 * only one immediate dominator, or none if it is the root). This tree is called
32 * a "dominator tree".
33 *
34 * This class represents a dominator tree constructed from a `JS::ubi::Node`
35 * heap graph. The domination relationship and dominator trees are useful tools
36 * for analyzing heap graphs because they tell you:
37 *
38 * - Exactly what could be reclaimed by the GC if some node `A` became
39 * unreachable: those nodes which are dominated by `A`,
40 *
41 * - The "retained size" of a node in the heap graph, in contrast to its
42 * "shallow size". The "shallow size" is the space taken by a node itself,
43 * not counting anything it references. The "retained size" of a node is its
44 * shallow size plus the size of all the things that would be collected if
45 * the original node wasn't (directly or indirectly) referencing them. In
46 * other words, the retained size is the shallow size of a node plus the
47 * shallow sizes of every other node it dominates. For example, the root
48 * node in a binary tree might have a small shallow size that does not take
49 * up much space itself, but it dominates the rest of the binary tree and
50 * its retained size is therefore significant (assuming no external
51 * references into the tree).
52 *
53 * The simple, engineered algorithm presented in "A Simple, Fast Dominance
54 * Algorithm" by Cooper el al[0] is used to find dominators and construct the
55 * dominator tree. This algorithm runs in O(n^2) time, but is faster in practice
56 * than alternative algorithms with better theoretical running times, such as
57 * Lengauer-Tarjan which runs in O(e * log(n)). The big caveat to that statement
58 * is that Cooper et al found it is faster in practice *on control flow graphs*
59 * and I'm not convinced that this property also holds on *heap* graphs. That
60 * said, the implementation of this algorithm is *much* simpler than
61 * Lengauer-Tarjan and has been found to be fast enough at least for the time
62 * being.
63 *
64 * [0]: http://www.cs.rice.edu/~keith/EMBED/dom.pdf
65 */
66class JS_PUBLIC_API DominatorTree {
67 private:
68 // Types.
69
70 using PredecessorSets = js::HashMap<Node, NodeSetPtr, js::DefaultHasher<Node>,
71 js::SystemAllocPolicy>;
72 using NodeToIndexMap = js::HashMap<Node, uint32_t, js::DefaultHasher<Node>,
73 js::SystemAllocPolicy>;
74 class DominatedSets;
75
76 public:
77 class DominatedSetRange;
78
79 /**
80 * A pointer to an immediately dominated node.
81 *
82 * Don't use this type directly; it is no safer than regular pointers. This
83 * is only for use indirectly with range-based for loops and
84 * `DominatedSetRange`.
85 *
86 * @see JS::ubi::DominatorTree::getDominatedSet
87 */
88 class DominatedNodePtr {
89 friend class DominatedSetRange;
90
91 const JS::ubi::Vector<Node>& postOrder;
92 const uint32_t* ptr;
93
94 DominatedNodePtr(const JS::ubi::Vector<Node>& postOrder,
95 const uint32_t* ptr)
96 : postOrder(postOrder), ptr(ptr) {}
97
98 public:
99 bool operator!=(const DominatedNodePtr& rhs) const {
100 return ptr != rhs.ptr;
101 }
102 void operator++() { ptr++; }
103 const Node& operator*() const { return postOrder[*ptr]; }
104 };
105
106 /**
107 * A range of immediately dominated `JS::ubi::Node`s for use with
108 * range-based for loops.
109 *
110 * @see JS::ubi::DominatorTree::getDominatedSet
111 */
112 class DominatedSetRange {
113 friend class DominatedSets;
114
115 const JS::ubi::Vector<Node>& postOrder;
116 const uint32_t* beginPtr;
117 const uint32_t* endPtr;
118
119 DominatedSetRange(JS::ubi::Vector<Node>& postOrder, const uint32_t* begin,
120 const uint32_t* end)
121 : postOrder(postOrder), beginPtr(begin), endPtr(end) {
122 MOZ_ASSERT(begin <= end)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(begin <= end)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(begin <= end))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("begin <= end"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 122); AnnotateMozCrashReason("MOZ_ASSERT" "(" "begin <= end"
")"); do { MOZ_CrashSequence(__null, 122); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
123 }
124
125 public:
126 DominatedNodePtr begin() const {
127 MOZ_ASSERT(beginPtr <= endPtr)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(beginPtr <= endPtr)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(beginPtr <= endPtr))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("beginPtr <= endPtr"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 127); AnnotateMozCrashReason("MOZ_ASSERT" "(" "beginPtr <= endPtr"
")"); do { MOZ_CrashSequence(__null, 127); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
128 return DominatedNodePtr(postOrder, beginPtr);
129 }
130
131 DominatedNodePtr end() const { return DominatedNodePtr(postOrder, endPtr); }
132
133 size_t length() const {
134 MOZ_ASSERT(beginPtr <= endPtr)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(beginPtr <= endPtr)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(beginPtr <= endPtr))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("beginPtr <= endPtr"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 134); AnnotateMozCrashReason("MOZ_ASSERT" "(" "beginPtr <= endPtr"
")"); do { MOZ_CrashSequence(__null, 134); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
135 return endPtr - beginPtr;
136 }
137
138 /**
139 * Safely skip ahead `n` dominators in the range, in O(1) time.
140 *
141 * Example usage:
142 *
143 * mozilla::Maybe<DominatedSetRange> range =
144 * myDominatorTree.getDominatedSet(myNode);
145 * if (range.isNothing()) {
146 * // Handle unknown nodes however you see fit...
147 * return false;
148 * }
149 *
150 * // Don't care about the first ten, for whatever reason.
151 * range->skip(10);
152 * for (const JS::ubi::Node& dominatedNode : *range) {
153 * // ...
154 * }
155 */
156 void skip(size_t n) {
157 beginPtr += n;
158 if (beginPtr > endPtr) {
159 beginPtr = endPtr;
160 }
161 }
162 };
163
164 private:
165 /**
166 * The set of all dominated sets in a dominator tree.
167 *
168 * Internally stores the sets in a contiguous array, with a side table of
169 * indices into that contiguous array to denote the start index of each
170 * individual set.
171 */
172 class DominatedSets {
173 JS::ubi::Vector<uint32_t> dominated;
174 JS::ubi::Vector<uint32_t> indices;
175
176 DominatedSets(JS::ubi::Vector<uint32_t>&& dominated,
177 JS::ubi::Vector<uint32_t>&& indices)
178 : dominated(std::move(dominated)), indices(std::move(indices)) {}
179
180 public:
181 // DominatedSets is not copy-able.
182 DominatedSets(const DominatedSets& rhs) = delete;
183 DominatedSets& operator=(const DominatedSets& rhs) = delete;
184
185 // DominatedSets is move-able.
186 DominatedSets(DominatedSets&& rhs)
187 : dominated(std::move(rhs.dominated)), indices(std::move(rhs.indices)) {
188 MOZ_ASSERT(this != &rhs, "self-move not allowed")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(this != &rhs)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(this != &rhs))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("this != &rhs"
" (" "self-move not allowed" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 188); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &rhs"
") (" "self-move not allowed" ")"); do { MOZ_CrashSequence(__null
, 188); __attribute__((nomerge)) ::abort(); } while (false); }
} while (false)
;
189 }
190 DominatedSets& operator=(DominatedSets&& rhs) {
191 this->~DominatedSets();
192 new (this) DominatedSets(std::move(rhs));
193 return *this;
194 }
195
196 /**
197 * Create the DominatedSets given the mapping of a node index to its
198 * immediate dominator. Returns `Some` on success, `Nothing` on OOM
199 * failure.
200 */
201 static mozilla::Maybe<DominatedSets> Create(
202 const JS::ubi::Vector<uint32_t>& doms) {
203 auto length = doms.length();
204 MOZ_ASSERT(length < UINT32_MAX)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(length < (4294967295U))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(length < (4294967295U))))
, 0))) { do { } while (false); MOZ_ReportAssertionFailure("length < (4294967295U)"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 204); AnnotateMozCrashReason("MOZ_ASSERT" "(" "length < (4294967295U)"
")"); do { MOZ_CrashSequence(__null, 204); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
205
206 // Create a vector `dominated` holding a flattened set of buckets of
207 // immediately dominated children nodes, with a lookup table
208 // `indices` mapping from each node to the beginning of its bucket.
209 //
210 // This has three phases:
211 //
212 // 1. Iterate over the full set of nodes and count up the size of
213 // each bucket. These bucket sizes are temporarily stored in the
214 // `indices` vector.
215 //
216 // 2. Convert the `indices` vector to store the cumulative sum of
217 // the sizes of all buckets before each index, resulting in a
218 // mapping from node index to one past the end of that node's
219 // bucket.
220 //
221 // 3. Iterate over the full set of nodes again, filling in bucket
222 // entries from the end of the bucket's range to its
223 // beginning. This decrements each index as a bucket entry is
224 // filled in. After having filled in all of a bucket's entries,
225 // the index points to the start of the bucket.
226
227 JS::ubi::Vector<uint32_t> dominated;
228 JS::ubi::Vector<uint32_t> indices;
229 if (!dominated.growBy(length) || !indices.growBy(length)) {
230 return mozilla::Nothing();
231 }
232
233 // 1
234 memset(indices.begin(), 0, length * sizeof(uint32_t));
235 for (uint32_t i = 0; i < length; i++) {
236 indices[doms[i]]++;
237 }
238
239 // 2
240 uint32_t sumOfSizes = 0;
241 for (uint32_t i = 0; i < length; i++) {
242 sumOfSizes += indices[i];
243 MOZ_ASSERT(sumOfSizes <= length)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(sumOfSizes <= length)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(sumOfSizes <= length))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("sumOfSizes <= length"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 243); AnnotateMozCrashReason("MOZ_ASSERT" "(" "sumOfSizes <= length"
")"); do { MOZ_CrashSequence(__null, 243); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
244 indices[i] = sumOfSizes;
245 }
246
247 // 3
248 for (uint32_t i = 0; i < length; i++) {
249 auto idxOfDom = doms[i];
250 indices[idxOfDom]--;
251 dominated[indices[idxOfDom]] = i;
252 }
253
254#ifdef DEBUG1
255 // Assert that our buckets are non-overlapping and don't run off the
256 // end of the vector.
257 uint32_t lastIndex = 0;
258 for (uint32_t i = 0; i < length; i++) {
259 MOZ_ASSERT(indices[i] >= lastIndex)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(indices[i] >= lastIndex)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(indices[i] >= lastIndex))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("indices[i] >= lastIndex"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 259); AnnotateMozCrashReason("MOZ_ASSERT" "(" "indices[i] >= lastIndex"
")"); do { MOZ_CrashSequence(__null, 259); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
260 MOZ_ASSERT(indices[i] < length)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(indices[i] < length)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(indices[i] < length))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("indices[i] < length"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 260); AnnotateMozCrashReason("MOZ_ASSERT" "(" "indices[i] < length"
")"); do { MOZ_CrashSequence(__null, 260); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
261 lastIndex = indices[i];
262 }
263#endif
264
265 return mozilla::Some(
266 DominatedSets(std::move(dominated), std::move(indices)));
267 }
268
269 /**
270 * Get the set of nodes immediately dominated by the node at
271 * `postOrder[nodeIndex]`.
272 */
273 DominatedSetRange dominatedSet(JS::ubi::Vector<Node>& postOrder,
274 uint32_t nodeIndex) const {
275 MOZ_ASSERT(postOrder.length() == indices.length())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(postOrder.length() == indices.length())>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(postOrder.length() == indices.length()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("postOrder.length() == indices.length()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 275); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == indices.length()"
")"); do { MOZ_CrashSequence(__null, 275); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
276 MOZ_ASSERT(nodeIndex < indices.length())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(nodeIndex < indices.length())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(nodeIndex < indices.length
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("nodeIndex < indices.length()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 276); AnnotateMozCrashReason("MOZ_ASSERT" "(" "nodeIndex < indices.length()"
")"); do { MOZ_CrashSequence(__null, 276); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
277 auto end = nodeIndex == indices.length() - 1
278 ? dominated.end()
279 : &dominated[indices[nodeIndex + 1]];
280 return DominatedSetRange(postOrder, &dominated[indices[nodeIndex]], end);
281 }
282 };
283
284 private:
285 // Data members.
286 JS::ubi::Vector<Node> postOrder;
287 NodeToIndexMap nodeToPostOrderIndex;
288 JS::ubi::Vector<uint32_t> doms;
289 DominatedSets dominatedSets;
290 mozilla::Maybe<JS::ubi::Vector<JS::ubi::Node::Size>> retainedSizes;
291
292 private:
293 // We use `UNDEFINED` as a sentinel value in the `doms` vector to signal
294 // that we haven't found any dominators for the node at the corresponding
295 // index in `postOrder` yet.
296 static const uint32_t UNDEFINED = UINT32_MAX(4294967295U);
297
298 DominatorTree(JS::ubi::Vector<Node>&& postOrder,
299 NodeToIndexMap&& nodeToPostOrderIndex,
300 JS::ubi::Vector<uint32_t>&& doms, DominatedSets&& dominatedSets)
301 : postOrder(std::move(postOrder)),
302 nodeToPostOrderIndex(std::move(nodeToPostOrderIndex)),
303 doms(std::move(doms)),
304 dominatedSets(std::move(dominatedSets)),
305 retainedSizes(mozilla::Nothing()) {}
306
307 static uint32_t intersect(JS::ubi::Vector<uint32_t>& doms, uint32_t finger1,
308 uint32_t finger2) {
309 while (finger1 != finger2) {
310 if (finger1 < finger2) {
311 finger1 = doms[finger1];
312 } else if (finger2 < finger1) {
313 finger2 = doms[finger2];
314 }
315 }
316 return finger1;
317 }
318
319 // Do the post order traversal of the heap graph and populate our
320 // predecessor sets.
321 [[nodiscard]] static bool doTraversal(JSContext* cx, AutoCheckCannotGC& noGC,
322 const Node& root,
323 JS::ubi::Vector<Node>& postOrder,
324 PredecessorSets& predecessorSets) {
325 uint32_t nodeCount = 0;
326 auto onNode = [&](const Node& node) {
327 nodeCount++;
328 if (MOZ_UNLIKELY(nodeCount == UINT32_MAX)(__builtin_expect(!!(nodeCount == (4294967295U)), 0))) {
329 return false;
330 }
331 return postOrder.append(node);
332 };
333
334 auto onEdge = [&](const Node& origin, const Edge& edge) {
335 auto p = predecessorSets.lookupForAdd(edge.referent);
336 if (!p) {
337 mozilla::UniquePtr<NodeSet, DeletePolicy<NodeSet>> set(
338 js_new<NodeSet>());
339 if (!set || !predecessorSets.add(p, edge.referent, std::move(set))) {
340 return false;
341 }
342 }
343 MOZ_ASSERT(p && p->value())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(p && p->value())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(p && p->value()))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("p && p->value()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 343); AnnotateMozCrashReason("MOZ_ASSERT" "(" "p && p->value()"
")"); do { MOZ_CrashSequence(__null, 343); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
344 return p->value()->put(origin);
345 };
346
347 PostOrder traversal(cx, noGC);
348 return traversal.addStart(root) && traversal.traverse(onNode, onEdge);
349 }
350
351 // Populates the given `map` with an entry for each node to its index in
352 // `postOrder`.
353 [[nodiscard]] static bool mapNodesToTheirIndices(
354 JS::ubi::Vector<Node>& postOrder, NodeToIndexMap& map) {
355 MOZ_ASSERT(map.empty())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(map.empty())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(map.empty()))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("map.empty()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 355); AnnotateMozCrashReason("MOZ_ASSERT" "(" "map.empty()"
")"); do { MOZ_CrashSequence(__null, 355); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
356 MOZ_ASSERT(postOrder.length() < UINT32_MAX)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(postOrder.length() < (4294967295U))>::isValid,
"invalid assertion condition"); if ((__builtin_expect(!!(!(!
!(postOrder.length() < (4294967295U)))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("postOrder.length() < (4294967295U)"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 356); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() < (4294967295U)"
")"); do { MOZ_CrashSequence(__null, 356); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
357 uint32_t length = postOrder.length();
358 if (!map.reserve(length)) {
359 return false;
360 }
361 for (uint32_t i = 0; i < length; i++) {
362 map.putNewInfallible(postOrder[i], i);
363 }
364 return true;
365 }
366
367 // Convert the Node -> NodeSet predecessorSets to a index -> Vector<index>
368 // form.
369 [[nodiscard]] static bool convertPredecessorSetsToVectors(
370 const Node& root, JS::ubi::Vector<Node>& postOrder,
371 PredecessorSets& predecessorSets, NodeToIndexMap& nodeToPostOrderIndex,
372 JS::ubi::Vector<JS::ubi::Vector<uint32_t>>& predecessorVectors) {
373 MOZ_ASSERT(postOrder.length() < UINT32_MAX)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(postOrder.length() < (4294967295U))>::isValid,
"invalid assertion condition"); if ((__builtin_expect(!!(!(!
!(postOrder.length() < (4294967295U)))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("postOrder.length() < (4294967295U)"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 373); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() < (4294967295U)"
")"); do { MOZ_CrashSequence(__null, 373); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
374 uint32_t length = postOrder.length();
375
376 MOZ_ASSERT(predecessorVectors.length() == 0)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(predecessorVectors.length() == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(predecessorVectors.length() ==
0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("predecessorVectors.length() == 0", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 376); AnnotateMozCrashReason("MOZ_ASSERT" "(" "predecessorVectors.length() == 0"
")"); do { MOZ_CrashSequence(__null, 376); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
377 if (!predecessorVectors.growBy(length)) {
378 return false;
379 }
380
381 for (uint32_t i = 0; i < length - 1; i++) {
382 auto& node = postOrder[i];
383 MOZ_ASSERT(node != root,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(node != root)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(node != root))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("node != root" " ("
"Only the last node should be root, since this was a post " "order traversal."
")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 385); AnnotateMozCrashReason("MOZ_ASSERT" "(" "node != root"
") (" "Only the last node should be root, since this was a post "
"order traversal." ")"); do { MOZ_CrashSequence(__null, 385)
; __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
384 "Only the last node should be root, since this was a post "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(node != root)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(node != root))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("node != root" " ("
"Only the last node should be root, since this was a post " "order traversal."
")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 385); AnnotateMozCrashReason("MOZ_ASSERT" "(" "node != root"
") (" "Only the last node should be root, since this was a post "
"order traversal." ")"); do { MOZ_CrashSequence(__null, 385)
; __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
385 "order traversal.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(node != root)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(node != root))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("node != root" " ("
"Only the last node should be root, since this was a post " "order traversal."
")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 385); AnnotateMozCrashReason("MOZ_ASSERT" "(" "node != root"
") (" "Only the last node should be root, since this was a post "
"order traversal." ")"); do { MOZ_CrashSequence(__null, 385)
; __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
;
386
387 auto ptr = predecessorSets.lookup(node);
388 MOZ_ASSERT(ptr,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(ptr)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("ptr" " (" "Because this isn't the root, it had better have "
"predecessors, or else how " "did we even find it." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 391); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "Because this isn't the root, it had better have "
"predecessors, or else how " "did we even find it." ")"); do
{ MOZ_CrashSequence(__null, 391); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
389 "Because this isn't the root, it had better have "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(ptr)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("ptr" " (" "Because this isn't the root, it had better have "
"predecessors, or else how " "did we even find it." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 391); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "Because this isn't the root, it had better have "
"predecessors, or else how " "did we even find it." ")"); do
{ MOZ_CrashSequence(__null, 391); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
390 "predecessors, or else how "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(ptr)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("ptr" " (" "Because this isn't the root, it had better have "
"predecessors, or else how " "did we even find it." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 391); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "Because this isn't the root, it had better have "
"predecessors, or else how " "did we even find it." ")"); do
{ MOZ_CrashSequence(__null, 391); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
391 "did we even find it.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(ptr)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("ptr" " (" "Because this isn't the root, it had better have "
"predecessors, or else how " "did we even find it." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 391); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "Because this isn't the root, it had better have "
"predecessors, or else how " "did we even find it." ")"); do
{ MOZ_CrashSequence(__null, 391); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
392
393 auto& predecessors = ptr->value();
394 if (!predecessorVectors[i].reserve(predecessors->count())) {
395 return false;
396 }
397 for (auto iter = predecessors->iter(); !iter.done(); iter.next()) {
398 auto ptr = nodeToPostOrderIndex.lookup(iter.get());
399 MOZ_ASSERT(ptr)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(ptr)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("ptr", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 399); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ")"); do
{ MOZ_CrashSequence(__null, 399); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
400 predecessorVectors[i].infallibleAppend(ptr->value());
401 }
402 }
403 predecessorSets.clearAndCompact();
404 return true;
405 }
406
407 // Initialize `doms` such that the immediate dominator of the `root` is the
408 // `root` itself and all others are `UNDEFINED`.
409 [[nodiscard]] static bool initializeDominators(
410 JS::ubi::Vector<uint32_t>& doms, uint32_t length) {
411 MOZ_ASSERT(doms.length() == 0)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(doms.length() == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(doms.length() == 0))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("doms.length() == 0"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 411); AnnotateMozCrashReason("MOZ_ASSERT" "(" "doms.length() == 0"
")"); do { MOZ_CrashSequence(__null, 411); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
412 if (!doms.growByUninitialized(length)) {
413 return false;
414 }
415 doms[length - 1] = length - 1;
416 for (uint32_t i = 0; i < length - 1; i++) {
417 doms[i] = UNDEFINED;
418 }
419 return true;
420 }
421
422 void assertSanity() const {
423 MOZ_ASSERT(postOrder.length() == doms.length())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(postOrder.length() == doms.length())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(postOrder.length() == doms.length
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("postOrder.length() == doms.length()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 423); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == doms.length()"
")"); do { MOZ_CrashSequence(__null, 423); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
424 MOZ_ASSERT(postOrder.length() == nodeToPostOrderIndex.count())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(postOrder.length() == nodeToPostOrderIndex.count())>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(postOrder.length() == nodeToPostOrderIndex.count()))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("postOrder.length() == nodeToPostOrderIndex.count()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 424); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == nodeToPostOrderIndex.count()"
")"); do { MOZ_CrashSequence(__null, 424); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
425 MOZ_ASSERT_IF(retainedSizes.isSome(),do { if (retainedSizes.isSome()) { do { static_assert( mozilla
::detail::AssertionConditionType<decltype(postOrder.length
() == retainedSizes->length())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(postOrder.length() == retainedSizes
->length()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("postOrder.length() == retainedSizes->length()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 426); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == retainedSizes->length()"
")"); do { MOZ_CrashSequence(__null, 426); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
426 postOrder.length() == retainedSizes->length())do { if (retainedSizes.isSome()) { do { static_assert( mozilla
::detail::AssertionConditionType<decltype(postOrder.length
() == retainedSizes->length())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(postOrder.length() == retainedSizes
->length()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("postOrder.length() == retainedSizes->length()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 426); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() == retainedSizes->length()"
")"); do { MOZ_CrashSequence(__null, 426); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
427 }
428
429 [[nodiscard]] bool computeRetainedSizes(mozilla::MallocSizeOf mallocSizeOf) {
430 MOZ_ASSERT(retainedSizes.isNothing())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(retainedSizes.isNothing())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(retainedSizes.isNothing())))
, 0))) { do { } while (false); MOZ_ReportAssertionFailure("retainedSizes.isNothing()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 430); AnnotateMozCrashReason("MOZ_ASSERT" "(" "retainedSizes.isNothing()"
")"); do { MOZ_CrashSequence(__null, 430); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
431 auto length = postOrder.length();
432
433 retainedSizes.emplace();
434 if (!retainedSizes->growBy(length)) {
435 retainedSizes = mozilla::Nothing();
436 return false;
437 }
438
439 // Iterate in forward order so that we know all of a node's children in
440 // the dominator tree have already had their retained size
441 // computed. Then we can simply say that the retained size of a node is
442 // its shallow size (JS::ubi::Node::size) plus the retained sizes of its
443 // immediate children in the tree.
444
445 for (uint32_t i = 0; i < length; i++) {
446 auto size = postOrder[i].size(mallocSizeOf);
447
448 for (const auto& dominated : dominatedSets.dominatedSet(postOrder, i)) {
449 // The root node dominates itself, but shouldn't contribute to
450 // its own retained size.
451 if (dominated == postOrder[length - 1]) {
452 MOZ_ASSERT(i == length - 1)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(i == length - 1)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(i == length - 1))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("i == length - 1"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 452); AnnotateMozCrashReason("MOZ_ASSERT" "(" "i == length - 1"
")"); do { MOZ_CrashSequence(__null, 452); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
453 continue;
454 }
455
456 auto ptr = nodeToPostOrderIndex.lookup(dominated);
457 MOZ_ASSERT(ptr)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(ptr)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("ptr", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 457); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ")"); do
{ MOZ_CrashSequence(__null, 457); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
458 auto idxOfDominated = ptr->value();
459 MOZ_ASSERT(idxOfDominated < i)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(idxOfDominated < i)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(idxOfDominated < i))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("idxOfDominated < i"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 459); AnnotateMozCrashReason("MOZ_ASSERT" "(" "idxOfDominated < i"
")"); do { MOZ_CrashSequence(__null, 459); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
460 size += retainedSizes.ref()[idxOfDominated];
461 }
462
463 retainedSizes.ref()[i] = size;
464 }
465
466 return true;
467 }
468
469 public:
470 // DominatorTree is not copy-able.
471 DominatorTree(const DominatorTree&) = delete;
472 DominatorTree& operator=(const DominatorTree&) = delete;
473
474 // DominatorTree is move-able.
475 DominatorTree(DominatorTree&& rhs)
476 : postOrder(std::move(rhs.postOrder)),
477 nodeToPostOrderIndex(std::move(rhs.nodeToPostOrderIndex)),
478 doms(std::move(rhs.doms)),
479 dominatedSets(std::move(rhs.dominatedSets)),
480 retainedSizes(std::move(rhs.retainedSizes)) {
481 MOZ_ASSERT(this != &rhs, "self-move is not allowed")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(this != &rhs)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(this != &rhs))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("this != &rhs"
" (" "self-move is not allowed" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 481); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &rhs"
") (" "self-move is not allowed" ")"); do { MOZ_CrashSequence
(__null, 481); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
;
482 }
483 DominatorTree& operator=(DominatorTree&& rhs) {
484 this->~DominatorTree();
485 new (this) DominatorTree(std::move(rhs));
486 return *this;
487 }
488
489 /**
490 * Construct a `DominatorTree` of the heap graph visible from `root`. The
491 * `root` is also used as the root of the resulting dominator tree.
492 *
493 * The resulting `DominatorTree` instance must not outlive the
494 * `JS::ubi::Node` graph it was constructed from.
495 *
496 * - For `JS::ubi::Node` graphs backed by the live heap graph, this means
497 * that the `DominatorTree`'s lifetime _must_ be contained within the
498 * scope of the provided `AutoCheckCannotGC` reference because a GC will
499 * invalidate the nodes.
500 *
501 * - For `JS::ubi::Node` graphs backed by some other offline structure
502 * provided by the embedder, the resulting `DominatorTree`'s lifetime is
503 * bounded by that offline structure's lifetime.
504 *
505 * In practice, this means that within SpiderMonkey we must treat
506 * `DominatorTree` as if it were backed by the live heap graph and trust
507 * that embedders with knowledge of the graph's implementation will do the
508 * Right Thing.
509 *
510 * Returns `mozilla::Nothing()` on OOM failure. It is the caller's
511 * responsibility to handle and report the OOM.
512 */
513 static mozilla::Maybe<DominatorTree> Create(JSContext* cx,
514 AutoCheckCannotGC& noGC,
515 const Node& root) {
516 JS::ubi::Vector<Node> postOrder;
517 PredecessorSets predecessorSets;
518 if (!doTraversal(cx, noGC, root, postOrder, predecessorSets)) {
519 return mozilla::Nothing();
520 }
521
522 MOZ_ASSERT(postOrder.length() < UINT32_MAX)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(postOrder.length() < (4294967295U))>::isValid,
"invalid assertion condition"); if ((__builtin_expect(!!(!(!
!(postOrder.length() < (4294967295U)))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("postOrder.length() < (4294967295U)"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 522); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder.length() < (4294967295U)"
")"); do { MOZ_CrashSequence(__null, 522); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
523 uint32_t length = postOrder.length();
524 MOZ_ASSERT(postOrder[length - 1] == root)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(postOrder[length - 1] == root)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(postOrder[length - 1] == root
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"postOrder[length - 1] == root", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 524); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder[length - 1] == root"
")"); do { MOZ_CrashSequence(__null, 524); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
525
526 // From here on out we wish to avoid hash table lookups, and we use
527 // indices into `postOrder` instead of actual nodes wherever
528 // possible. This greatly improves the performance of this
529 // implementation, but we have to pay a little bit of upfront cost to
530 // convert our data structures to play along first.
531
532 NodeToIndexMap nodeToPostOrderIndex(postOrder.length());
533 if (!mapNodesToTheirIndices(postOrder, nodeToPostOrderIndex)) {
534 return mozilla::Nothing();
535 }
536
537 JS::ubi::Vector<JS::ubi::Vector<uint32_t>> predecessorVectors;
538 if (!convertPredecessorSetsToVectors(root, postOrder, predecessorSets,
539 nodeToPostOrderIndex,
540 predecessorVectors))
541 return mozilla::Nothing();
542
543 JS::ubi::Vector<uint32_t> doms;
544 if (!initializeDominators(doms, length)) {
545 return mozilla::Nothing();
546 }
547
548 bool changed = true;
549 while (changed) {
550 changed = false;
551
552 // Iterate over the non-root nodes in reverse post order.
553 for (uint32_t indexPlusOne = length - 1; indexPlusOne > 0;
554 indexPlusOne--) {
555 MOZ_ASSERT(postOrder[indexPlusOne - 1] != root)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(postOrder[indexPlusOne - 1] != root)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(postOrder[indexPlusOne - 1] !=
root))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("postOrder[indexPlusOne - 1] != root", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 555); AnnotateMozCrashReason("MOZ_ASSERT" "(" "postOrder[indexPlusOne - 1] != root"
")"); do { MOZ_CrashSequence(__null, 555); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
556
557 // Take the intersection of every predecessor's dominator set;
558 // that is the current best guess at the immediate dominator for
559 // this node.
560
561 uint32_t newIDomIdx = UNDEFINED;
562
563 auto& predecessors = predecessorVectors[indexPlusOne - 1];
564 auto range = predecessors.all();
565 for (; !range.empty(); range.popFront()) {
566 auto idx = range.front();
567 if (doms[idx] != UNDEFINED) {
568 newIDomIdx = idx;
569 break;
570 }
571 }
572
573 MOZ_ASSERT(newIDomIdx != UNDEFINED,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED"
" (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 578); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED"
") (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")"); do { MOZ_CrashSequence
(__null, 578); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
574 "Because the root is initialized to dominate itself and is "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED"
" (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 578); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED"
") (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")"); do { MOZ_CrashSequence
(__null, 578); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
575 "the first "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED"
" (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 578); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED"
") (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")"); do { MOZ_CrashSequence
(__null, 578); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
576 "node in every path, there must exist a predecessor to this "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED"
" (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 578); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED"
") (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")"); do { MOZ_CrashSequence
(__null, 578); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
577 "node that "do { static_assert( mozilla::detail::AssertionConditionType<
decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED"
" (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 578); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED"
") (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")"); do { MOZ_CrashSequence
(__null, 578); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
578 "also has a dominator.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(newIDomIdx != UNDEFINED)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(newIDomIdx != UNDEFINED))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("newIDomIdx != UNDEFINED"
" (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 578); AnnotateMozCrashReason("MOZ_ASSERT" "(" "newIDomIdx != UNDEFINED"
") (" "Because the root is initialized to dominate itself and is "
"the first " "node in every path, there must exist a predecessor to this "
"node that " "also has a dominator." ")"); do { MOZ_CrashSequence
(__null, 578); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
;
579
580 for (; !range.empty(); range.popFront()) {
581 auto idx = range.front();
582 if (doms[idx] != UNDEFINED) {
583 newIDomIdx = intersect(doms, newIDomIdx, idx);
584 }
585 }
586
587 // If the immediate dominator changed, we will have to do
588 // another pass of the outer while loop to continue the forward
589 // dataflow.
590 if (newIDomIdx != doms[indexPlusOne - 1]) {
591 doms[indexPlusOne - 1] = newIDomIdx;
592 changed = true;
593 }
594 }
595 }
596
597 auto maybeDominatedSets = DominatedSets::Create(doms);
598 if (maybeDominatedSets.isNothing()) {
599 return mozilla::Nothing();
600 }
601
602 return mozilla::Some(
603 DominatorTree(std::move(postOrder), std::move(nodeToPostOrderIndex),
604 std::move(doms), std::move(*maybeDominatedSets)));
605 }
606
607 /**
608 * Get the root node for this dominator tree.
609 */
610 const Node& root() const { return postOrder[postOrder.length() - 1]; }
611
612 /**
613 * Return the immediate dominator of the given `node`. If `node` was not
614 * reachable from the `root` that this dominator tree was constructed from,
615 * then return the null `JS::ubi::Node`.
616 */
617 Node getImmediateDominator(const Node& node) const {
618 assertSanity();
619 auto ptr = nodeToPostOrderIndex.lookup(node);
4
Calling 'HashMap::lookup'
13
Returning from 'HashMap::lookup'
620 if (!ptr) {
14
Assuming the condition is false
15
Taking false branch
621 return Node();
622 }
623
624 auto idx = ptr->value();
16
Calling 'Ptr::operator->'
625 MOZ_ASSERT(idx < postOrder.length())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(idx < postOrder.length())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(idx < postOrder.length())
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("idx < postOrder.length()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 625); AnnotateMozCrashReason("MOZ_ASSERT" "(" "idx < postOrder.length()"
")"); do { MOZ_CrashSequence(__null, 625); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
626 return postOrder[doms[idx]];
627 }
628
629 /**
630 * Get the set of nodes immediately dominated by the given `node`. If `node`
631 * is not a member of this dominator tree, return `Nothing`.
632 *
633 * Example usage:
634 *
635 * mozilla::Maybe<DominatedSetRange> range =
636 * myDominatorTree.getDominatedSet(myNode);
637 * if (range.isNothing()) {
638 * // Handle unknown node however you see fit...
639 * return false;
640 * }
641 *
642 * for (const JS::ubi::Node& dominatedNode : *range) {
643 * // Do something with each immediately dominated node...
644 * }
645 */
646 mozilla::Maybe<DominatedSetRange> getDominatedSet(const Node& node) {
647 assertSanity();
648 auto ptr = nodeToPostOrderIndex.lookup(node);
649 if (!ptr) {
650 return mozilla::Nothing();
651 }
652
653 auto idx = ptr->value();
654 MOZ_ASSERT(idx < postOrder.length())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(idx < postOrder.length())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(idx < postOrder.length())
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("idx < postOrder.length()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 654); AnnotateMozCrashReason("MOZ_ASSERT" "(" "idx < postOrder.length()"
")"); do { MOZ_CrashSequence(__null, 654); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
655 return mozilla::Some(dominatedSets.dominatedSet(postOrder, idx));
656 }
657
658 /**
659 * Get the retained size of the given `node`. The size is placed in
660 * `outSize`, or 0 if `node` is not a member of the dominator tree. Returns
661 * false on OOM failure, leaving `outSize` unchanged.
662 */
663 [[nodiscard]] bool getRetainedSize(const Node& node,
664 mozilla::MallocSizeOf mallocSizeOf,
665 Node::Size& outSize) {
666 assertSanity();
667 auto ptr = nodeToPostOrderIndex.lookup(node);
668 if (!ptr) {
669 outSize = 0;
670 return true;
671 }
672
673 if (retainedSizes.isNothing() && !computeRetainedSizes(mallocSizeOf)) {
674 return false;
675 }
676
677 auto idx = ptr->value();
678 MOZ_ASSERT(idx < postOrder.length())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(idx < postOrder.length())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(idx < postOrder.length())
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("idx < postOrder.length()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeDominatorTree.h"
, 678); AnnotateMozCrashReason("MOZ_ASSERT" "(" "idx < postOrder.length()"
")"); do { MOZ_CrashSequence(__null, 678); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
679 outSize = retainedSizes.ref()[idx];
680 return true;
681 }
682};
683
684} // namespace ubi
685} // namespace JS
686
687#endif // js_UbiNodeDominatorTree_h

←

/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//---------------------------------------------------------------------------
6// Overview
7//---------------------------------------------------------------------------
8//
9// This file defines HashMap<Key, Value> and HashSet<T>, hash tables that are
10// fast and have a nice API.
11//
12// Both hash tables have two optional template parameters.
13//
14// - HashPolicy. This defines the operations for hashing and matching keys. The
15// default HashPolicy is appropriate when both of the following two
16// conditions are true.
17//
18// - The key type stored in the table (|Key| for |HashMap<Key, Value>|, |T|
19// for |HashSet<T>|) is an integer, pointer, UniquePtr, float, or double.
20//
21// - The type used for lookups (|Lookup|) is the same as the key type. This
22// is usually the case, but not always.
23//
24// There is also a |CStringHasher| policy for |char*| keys. If your keys
25// don't match any of the above cases, you must provide your own hash policy;
26// see the "Hash Policy" section below.
27//
28// - AllocPolicy. This defines how allocations are done by the table.
29//
30// - |MallocAllocPolicy| is the default and is usually appropriate; note that
31// operations (such as insertions) that might cause allocations are
32// fallible and must be checked for OOM. These checks are enforced by the
33// use of [[nodiscard]].
34//
35// - |InfallibleAllocPolicy| is another possibility; it allows the
36// abovementioned OOM checks to be done with MOZ_ALWAYS_TRUE().
37//
38// Note that entry storage allocation is lazy, and not done until the first
39// lookupForAdd(), put(), or putNew() is performed.
40//
41// See AllocPolicy.h for more details.
42//
43// Documentation on how to use HashMap and HashSet, including examples, is
44// present within those classes. Search for "class HashMap" and "class
45// HashSet".
46//
47// Both HashMap and HashSet are implemented on top of a third class, HashTable.
48// You only need to look at HashTable if you want to understand the
49// implementation.
50//
51// How does mozilla::HashTable (this file) compare with PLDHashTable (and its
52// subclasses, such as nsTHashtable)?
53//
54// - mozilla::HashTable is a lot faster, largely because it uses templates
55// throughout *and* inlines everything. PLDHashTable inlines operations much
56// less aggressively, and also uses "virtual ops" for operations like hashing
57// and matching entries that require function calls.
58//
59// - Correspondingly, mozilla::HashTable use is likely to increase executable
60// size much more than PLDHashTable.
61//
62// - mozilla::HashTable has a nicer API, with a proper HashSet vs. HashMap
63// distinction.
64//
65// - mozilla::HashTable requires more explicit OOM checking. As mentioned
66// above, the use of |InfallibleAllocPolicy| can simplify things.
67//
68// - mozilla::HashTable has a default capacity on creation of 32 and a minimum
69// capacity of 4. PLDHashTable has a default capacity on creation of 8 and a
70// minimum capacity of 8.
71
72#ifndef mozilla_HashTable_h
73#define mozilla_HashTable_h
74
75#include <bit>
76#include <type_traits>
77#include <utility>
78
79#include "mozilla/AllocPolicy.h"
80#include "mozilla/Assertions.h"
81#include "mozilla/Attributes.h"
82#include "mozilla/Casting.h"
83#include "mozilla/HashFunctions.h"
84#include "mozilla/MathAlgorithms.h"
85#include "mozilla/Maybe.h"
86#include "mozilla/MemoryChecking.h"
87#include "mozilla/MemoryReporting.h"
88#include "mozilla/Opaque.h"
89#include "mozilla/OperatorNewExtensions.h"
90#include "mozilla/ReentrancyGuard.h"
91#include "mozilla/UniquePtr.h"
92#include "mozilla/WrappingOperations.h"
93
94namespace mozilla {
95
96template <class, class = void>
97struct DefaultHasher;
98
99template <class, class>
100class HashMapEntry;
101
102namespace detail {
103
104template <typename T>
105class HashTableEntry;
106
107template <class T, class HashPolicy, class AllocPolicy>
108class HashTable;
109
110} // namespace detail
111
112// The "generation" of a hash table is an opaque value indicating the state of
113// modification of the hash table through its lifetime. If the generation of
114// a hash table compares equal at times T1 and T2, then lookups in the hash
115// table, pointers to (or into) hash table entries, etc. at time T1 are valid
116// at time T2. If the generation compares unequal, these computations are all
117// invalid and must be performed again to be used.
118//
119// Generations are meaningfully comparable only with respect to a single hash
120// table. It's always nonsensical to compare the generation of distinct hash
121// tables H1 and H2.
122using Generation = Opaque<uint64_t>;
123
124//---------------------------------------------------------------------------
125// HashMap
126//---------------------------------------------------------------------------
127
128// HashMap is a fast hash-based map from keys to values.
129//
130// Template parameter requirements:
131// - Key/Value: movable, destructible, assignable.
132// - HashPolicy: see the "Hash Policy" section below.
133// - AllocPolicy: see AllocPolicy.h.
134//
135// Note:
136// - HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members
137// called by HashMap must not call back into the same HashMap object.
138//
139template <class Key, class Value, class HashPolicy = DefaultHasher<Key>,
140 class AllocPolicy = MallocAllocPolicy>
141class MOZ_STANDALONE_DEBUG[[clang::standalone_debug]] HashMap {
142 // -- Implementation details -----------------------------------------------
143
144 using TableEntry = HashMapEntry<Key, Value>;
145
146 struct MapHashPolicy : HashPolicy {
147 using Base = HashPolicy;
148 using KeyType = Key;
149
150 static const Key& getKey(TableEntry& aEntry) { return aEntry.key(); }
151
152 template <typename KeyInput>
153 static void setKey(TableEntry& aEntry, KeyInput&& aKey) {
154 HashPolicy::rekey(aEntry.mutableKey(), std::forward<KeyInput>(aKey));
155 }
156 };
157
158 using Impl = detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy>;
159 Impl mImpl;
160
161 public:
162 using Lookup = typename HashPolicy::Lookup;
163 using Entry = TableEntry;
164
165 // -- Initialization -------------------------------------------------------
166
167 constexpr explicit HashMap(AllocPolicy aAllocPolicy = AllocPolicy(),
168 uint32_t aLen = Impl::sDefaultLen)
169 : mImpl(std::move(aAllocPolicy), aLen) {}
170
171 explicit HashMap(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {}
172
173 // HashMap is movable.
174 HashMap(HashMap&& aRhs) = default;
175 HashMap& operator=(HashMap&& aRhs) = default;
176
177 // Swap the contents of this hash map with another.
178 void swap(HashMap& aOther) { mImpl.swap(aOther.mImpl); }
179
180 // -- Status and sizing ----------------------------------------------------
181
182 // The map's current generation.
183 Generation generation() const { return mImpl.generation(); }
184
185 // Is the map empty?
186 bool empty() const { return mImpl.empty(); }
187
188 // Number of keys/values in the map.
189 uint32_t count() const { return mImpl.count(); }
190
191 // Number of key/value slots in the map. Note: resize will happen well before
192 // count() == capacity().
193 uint32_t capacity() const { return mImpl.capacity(); }
194
195 // The size of the map's entry storage, in bytes. If the keys/values contain
196 // pointers to other heap blocks, you must iterate over the map and measure
197 // them separately; hence the "shallow" prefix.
198 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
199 return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
200 }
201 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
202 return aMallocSizeOf(this) +
203 mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
204 }
205
206 // Attempt to minimize the capacity(). If the table is empty, this will free
207 // the empty storage and upon regrowth it will be given the minimum capacity.
208 void compact() { mImpl.compact(); }
209
210 // Attempt to reserve enough space to fit at least |aLen| elements. This is
211 // total capacity, including elements already present. Does nothing if the
212 // map already has sufficient capacity.
213 [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); }
214
215 // HashMap is not copyable or assignable.
216 HashMap(const HashMap& hm) = delete;
217 HashMap& operator=(const HashMap& hm) = delete;
218
219 // -- Lookups --------------------------------------------------------------
220
221 // Does the map contain a key/value matching |aLookup|?
222 bool has(const Lookup& aLookup) const {
223 return mImpl.lookup(aLookup).found();
224 }
225
226 // Return a Ptr indicating whether a key/value matching |aLookup| is
227 // present in the map. E.g.:
228 //
229 // using HM = HashMap<int,char>;
230 // HM h;
231 // if (HM::Ptr p = h.lookup(3)) {
232 // assert(p->key() == 3);
233 // char val = p->value();
234 // }
235 //
236 using Ptr = typename Impl::Ptr;
237 MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const {
238 return mImpl.lookup(aLookup);
5
Calling 'HashTable::lookup'
12
Returning from 'HashTable::lookup'
239 }
240
241 // Like lookup(), but does not assert if two threads call it at the same
242 // time. Only use this method when none of the threads will modify the map.
243 MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const {
244 return mImpl.readonlyThreadsafeLookup(aLookup);
245 }
246
247 // -- Insertions -----------------------------------------------------------
248
249 // Overwrite existing value with |aValue|, or add it if not present. Returns
250 // false on OOM.
251 template <typename KeyInput, typename ValueInput>
252 [[nodiscard]] bool put(KeyInput&& aKey, ValueInput&& aValue) {
253 return put(aKey, std::forward<KeyInput>(aKey),
254 std::forward<ValueInput>(aValue));
255 }
256
257 template <typename KeyInput, typename ValueInput>
258 [[nodiscard]] bool put(const Lookup& aLookup, KeyInput&& aKey,
259 ValueInput&& aValue) {
260 AddPtr p = lookupForAdd(aLookup);
261 if (p) {
262 p->value() = std::forward<ValueInput>(aValue);
263 return true;
264 }
265 return add(p, std::forward<KeyInput>(aKey),
266 std::forward<ValueInput>(aValue));
267 }
268
269 // Like put(), but slightly faster. Must only be used when the given key is
270 // not already present. (In debug builds, assertions check this.)
271 template <typename KeyInput, typename ValueInput>
272 [[nodiscard]] bool putNew(KeyInput&& aKey, ValueInput&& aValue) {
273 return mImpl.putNew(aKey, std::forward<KeyInput>(aKey),
274 std::forward<ValueInput>(aValue));
275 }
276
277 template <typename KeyInput, typename ValueInput>
278 [[nodiscard]] bool putNew(const Lookup& aLookup, KeyInput&& aKey,
279 ValueInput&& aValue) {
280 return mImpl.putNew(aLookup, std::forward<KeyInput>(aKey),
281 std::forward<ValueInput>(aValue));
282 }
283
284 // Like putNew(), but should be only used when the table is known to be big
285 // enough for the insertion, and hashing cannot fail. Typically this is used
286 // to populate an empty map with known-unique keys after reserving space with
287 // reserve(), e.g.
288 //
289 // using HM = HashMap<int,char>;
290 // HM h;
291 // if (!h.reserve(3)) {
292 // MOZ_CRASH("OOM");
293 // }
294 // h.putNewInfallible(1, 'a'); // unique key
295 // h.putNewInfallible(2, 'b'); // unique key
296 // h.putNewInfallible(3, 'c'); // unique key
297 //
298 template <typename KeyInput, typename ValueInput>
299 void putNewInfallible(KeyInput&& aKey, ValueInput&& aValue) {
300 mImpl.putNewInfallible(aKey, std::forward<KeyInput>(aKey),
301 std::forward<ValueInput>(aValue));
302 }
303
304 // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
305 // insertion of Key |k| (where |HashPolicy::match(k,l) == true|) using
306 // |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new key/value. E.g.:
307 //
308 // using HM = HashMap<int,char>;
309 // HM h;
310 // HM::AddPtr p = h.lookupForAdd(3);
311 // if (!p) {
312 // if (!h.add(p, 3, 'a')) {
313 // return false;
314 // }
315 // }
316 // assert(p->key() == 3);
317 // char val = p->value();
318 //
319 // N.B. The caller must ensure that no mutating hash table operations occur
320 // between a pair of lookupForAdd() and add() calls. To avoid looking up the
321 // key a second time, the caller may use the more efficient relookupOrAdd()
322 // method. This method reuses part of the hashing computation to more
323 // efficiently insert the key if it has not been added. For example, a
324 // mutation-handling version of the previous example:
325 //
326 // HM::AddPtr p = h.lookupForAdd(3);
327 // if (!p) {
328 // call_that_may_mutate_h();
329 // if (!h.relookupOrAdd(p, 3, 'a')) {
330 // return false;
331 // }
332 // }
333 // assert(p->key() == 3);
334 // char val = p->value();
335 //
336 using AddPtr = typename Impl::AddPtr;
337 MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) {
338 return mImpl.lookupForAdd(aLookup);
339 }
340
341 // Add a key/value. Returns false on OOM.
342 template <typename KeyInput, typename ValueInput>
343 [[nodiscard]] bool add(AddPtr& aPtr, KeyInput&& aKey, ValueInput&& aValue) {
344 return mImpl.add(aPtr, std::forward<KeyInput>(aKey),
345 std::forward<ValueInput>(aValue));
346 }
347
348 // See the comment above lookupForAdd() for details.
349 template <typename KeyInput, typename ValueInput>
350 [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, KeyInput&& aKey,
351 ValueInput&& aValue) {
352 return mImpl.relookupOrAdd(aPtr, aKey, std::forward<KeyInput>(aKey),
353 std::forward<ValueInput>(aValue));
354 }
355
356 // -- Removal --------------------------------------------------------------
357
358 // Lookup and remove the key/value matching |aLookup|, if present.
359 void remove(const Lookup& aLookup) {
360 if (Ptr p = lookup(aLookup)) {
361 remove(p);
362 }
363 }
364
365 // Remove a previously found key/value (assuming aPtr.found()). The map must
366 // not have been mutated in the interim.
367 void remove(Ptr aPtr) { mImpl.remove(aPtr); }
368
369 // Remove all keys/values without changing the capacity.
370 void clear() { mImpl.clear(); }
371
372 // Like clear() followed by compact().
373 void clearAndCompact() { mImpl.clearAndCompact(); }
374
375 // -- Rekeying -------------------------------------------------------------
376
377 // Infallibly rekey one entry, if necessary. Requires Key to be rekeyable (via
378 // to HashPolicy::rekey) from Lookup.
379 void rekeyIfMoved(const Lookup& aOldKey, const Lookup& aNewKeyInput) {
380 if (aOldKey != aNewKeyInput) {
381 rekeyAs(aOldKey, aNewKeyInput, aNewKeyInput);
382 }
383 }
384
385 // Infallibly rekey one entry if present, and return whether that happened.
386 template <typename KeyInput>
387 bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup,
388 KeyInput&& aNewKey) {
389 if (Ptr p = lookup(aOldLookup)) {
390 mImpl.rekeyAndMaybeRehash(p, aNewLookup, std::forward<KeyInput>(aNewKey));
391 return true;
392 }
393 return false;
394 }
395
396 // -- Iteration ------------------------------------------------------------
397
398 // |iter()| returns an Iterator:
399 //
400 // HashMap<int, char> h;
401 // for (auto iter = h.iter(); !iter.done(); iter.next()) {
402 // char c = iter.get().value();
403 // }
404 //
405 using Iterator = typename Impl::Iterator;
406 Iterator iter() const { return mImpl.iter(); }
407
408 // |modIter()| returns a ModIterator:
409 //
410 // HashMap<int, char> h;
411 // for (auto iter = h.modIter(); !iter.done(); iter.next()) {
412 // if (iter.get().value() == 'l') {
413 // iter.remove();
414 // }
415 // }
416 //
417 // Table resize may occur in ModIterator's destructor.
418 using ModIterator = typename Impl::ModIterator;
419 ModIterator modIter() { return mImpl.modIter(); }
420
421 // -- Alloc policy ---------------------------------------------------------
422
423 // Get the alloc policy.
424 const AllocPolicy& allocPolicy() const { return mImpl.allocPolicy(); }
425 AllocPolicy& allocPolicy() { return mImpl.allocPolicy(); }
426
427 // For internal use by allocation policies that provide garbage collected
428 // memory.
429 //
430 // Trace any allocations owned by this object that were made with AllocPolicy.
431 // Call the supplied closure |aTraceFunc| for each of them, passing a double
432 // pointer to the memory held (e.g. a void** pointer).
433 template <typename F>
434 void traceOwnedAllocs(F&& aTraceFunc) {
435 mImpl.traceOwnedAllocs(std::forward<F>(aTraceFunc));
436 }
437
438 // -- Layout information for JIT access ------------------------------------
439
440 static size_t offsetOfHashShift() {
441 return offsetof(HashMap, mImpl)__builtin_offsetof(HashMap, mImpl) + Impl::offsetOfHashShift();
442 }
443 static size_t offsetOfTable() {
444 return offsetof(HashMap, mImpl)__builtin_offsetof(HashMap, mImpl) + Impl::offsetOfTable();
445 }
446 static size_t offsetOfEntryCount() {
447 return offsetof(HashMap, mImpl)__builtin_offsetof(HashMap, mImpl) + Impl::offsetOfEntryCount();
448 }
449};
450
451//---------------------------------------------------------------------------
452// HashSet
453//---------------------------------------------------------------------------
454
455// HashSet is a fast hash-based set of values.
456//
457// Template parameter requirements:
458// - T: movable, destructible, assignable.
459// - HashPolicy: see the "Hash Policy" section below.
460// - AllocPolicy: see AllocPolicy.h
461//
462// Note:
463// - HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by
464// HashSet must not call back into the same HashSet object.
465//
466template <class T, class HashPolicy = DefaultHasher<T>,
467 class AllocPolicy = MallocAllocPolicy>
468class HashSet {
469 // -- Implementation details -----------------------------------------------
470
471 struct SetHashPolicy : HashPolicy {
472 using Base = HashPolicy;
473 using KeyType = T;
474
475 static const KeyType& getKey(const T& aT) { return aT; }
476
477 template <typename KeyInput>
478 static void setKey(T& aT, KeyInput&& aKey) {
479 HashPolicy::rekey(aT, std::forward<KeyInput>(aKey));
480 }
481 };
482
483 using Impl = detail::HashTable<const T, SetHashPolicy, AllocPolicy>;
484 Impl mImpl;
485
486 public:
487 using Lookup = typename HashPolicy::Lookup;
488 using Entry = T;
489
490 // -- Initialization -------------------------------------------------------
491
492 explicit HashSet(AllocPolicy aAllocPolicy = AllocPolicy(),
493 uint32_t aLen = Impl::sDefaultLen)
494 : mImpl(std::move(aAllocPolicy), aLen) {}
495
496 explicit HashSet(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {}
497
498 // HashSet is movable.
499 HashSet(HashSet&& aRhs) = default;
500 HashSet& operator=(HashSet&& aRhs) = default;
501
502 // HashSet is not copyable or assignable.
503 HashSet(const HashSet& hs) = delete;
504 HashSet& operator=(const HashSet& hs) = delete;
505
506 // Swap the contents of this hash set with another.
507 void swap(HashSet& aOther) { mImpl.swap(aOther.mImpl); }
508
509 // -- Status and sizing ----------------------------------------------------
510
511 // The set's current generation.
512 Generation generation() const { return mImpl.generation(); }
513
514 // Is the set empty?
515 bool empty() const { return mImpl.empty(); }
516
517 // Number of elements in the set.
518 uint32_t count() const { return mImpl.count(); }
519
520 // Number of element slots in the set. Note: resize will happen well before
521 // count() == capacity().
522 uint32_t capacity() const { return mImpl.capacity(); }
523
524 // The size of the set's entry storage, in bytes. If the elements contain
525 // pointers to other heap blocks, you must iterate over the set and measure
526 // them separately; hence the "shallow" prefix.
527 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
528 return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
529 }
530 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
531 return aMallocSizeOf(this) +
532 mImpl.shallowSizeOfExcludingThis(aMallocSizeOf);
533 }
534
535 // Attempt to minimize the capacity(). If the table is empty, this will free
536 // the empty storage and upon regrowth it will be given the minimum capacity.
537 void compact() { mImpl.compact(); }
538
539 // Attempt to reserve enough space to fit at least |aLen| elements. This is
540 // total capacity, including elements already present. Does nothing if the
541 // map already has sufficient capacity.
542 [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); }
543
544 // -- Lookups --------------------------------------------------------------
545
546 // Does the set contain an element matching |aLookup|?
547 bool has(const Lookup& aLookup) const {
548 return mImpl.lookup(aLookup).found();
549 }
550
551 // Return a Ptr indicating whether an element matching |aLookup| is present
552 // in the set. E.g.:
553 //
554 // using HS = HashSet<int>;
555 // HS h;
556 // if (HS::Ptr p = h.lookup(3)) {
557 // assert(*p == 3); // p acts like a pointer to int
558 // }
559 //
560 using Ptr = typename Impl::Ptr;
561 MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const {
562 return mImpl.lookup(aLookup);
563 }
564
565 // Like lookup(), but does not assert if two threads call it at the same
566 // time. Only use this method when none of the threads will modify the set.
567 MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const {
568 return mImpl.readonlyThreadsafeLookup(aLookup);
569 }
570
571 // -- Insertions -----------------------------------------------------------
572
573 // Add |aU| if it is not present already. Returns false on OOM.
574 template <typename U>
575 [[nodiscard]] bool put(U&& aU) {
576 AddPtr p = lookupForAdd(aU);
577 return p ? true : add(p, std::forward<U>(aU));
578 }
579
580 // Like put(), but slightly faster. Must only be used when the given element
581 // is not already present. (In debug builds, assertions check this.)
582 template <typename U>
583 [[nodiscard]] bool putNew(U&& aU) {
584 return mImpl.putNew(aU, std::forward<U>(aU));
585 }
586
587 // Like the other putNew(), but for when |Lookup| is different to |T|.
588 template <typename U>
589 [[nodiscard]] bool putNew(const Lookup& aLookup, U&& aU) {
590 return mImpl.putNew(aLookup, std::forward<U>(aU));
591 }
592
593 // Like putNew(), but should be only used when the table is known to be big
594 // enough for the insertion, and hashing cannot fail. Typically this is used
595 // to populate an empty set with known-unique elements after reserving space
596 // with reserve(), e.g.
597 //
598 // using HS = HashMap<int>;
599 // HS h;
600 // if (!h.reserve(3)) {
601 // MOZ_CRASH("OOM");
602 // }
603 // h.putNewInfallible(1); // unique element
604 // h.putNewInfallible(2); // unique element
605 // h.putNewInfallible(3); // unique element
606 //
607 template <typename U>
608 void putNewInfallible(const Lookup& aLookup, U&& aU) {
609 mImpl.putNewInfallible(aLookup, std::forward<U>(aU));
610 }
611
612 // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
613 // insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using
614 // |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.:
615 //
616 // using HS = HashSet<int>;
617 // HS h;
618 // HS::AddPtr p = h.lookupForAdd(3);
619 // if (!p) {
620 // if (!h.add(p, 3)) {
621 // return false;
622 // }
623 // }
624 // assert(*p == 3); // p acts like a pointer to int
625 //
626 // N.B. The caller must ensure that no mutating hash table operations occur
627 // between a pair of lookupForAdd() and add() calls. To avoid looking up the
628 // key a second time, the caller may use the more efficient relookupOrAdd()
629 // method. This method reuses part of the hashing computation to more
630 // efficiently insert the key if it has not been added. For example, a
631 // mutation-handling version of the previous example:
632 //
633 // HS::AddPtr p = h.lookupForAdd(3);
634 // if (!p) {
635 // call_that_may_mutate_h();
636 // if (!h.relookupOrAdd(p, 3, 3)) {
637 // return false;
638 // }
639 // }
640 // assert(*p == 3);
641 //
642 // Note that relookupOrAdd(p,l,t) performs Lookup using |l| and adds the
643 // entry |t|, where the caller ensures match(l,t).
644 using AddPtr = typename Impl::AddPtr;
645 MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) {
646 return mImpl.lookupForAdd(aLookup);
647 }
648
649 // Add an element. Returns false on OOM.
650 template <typename U>
651 [[nodiscard]] bool add(AddPtr& aPtr, U&& aU) {
652 return mImpl.add(aPtr, std::forward<U>(aU));
653 }
654
655 // See the comment above lookupForAdd() for details.
656 template <typename U>
657 [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup,
658 U&& aU) {
659 return mImpl.relookupOrAdd(aPtr, aLookup, std::forward<U>(aU));
660 }
661
662 // -- Removal --------------------------------------------------------------
663
664 // Lookup and remove the element matching |aLookup|, if present.
665 void remove(const Lookup& aLookup) {
666 if (Ptr p = lookup(aLookup)) {
667 remove(p);
668 }
669 }
670
671 // Remove a previously found element (assuming aPtr.found()). The set must
672 // not have been mutated in the interim.
673 void remove(Ptr aPtr) { mImpl.remove(aPtr); }
674
675 // Remove all keys/values without changing the capacity.
676 void clear() { mImpl.clear(); }
677
678 // Like clear() followed by compact().
679 void clearAndCompact() { mImpl.clearAndCompact(); }
680
681 // -- Rekeying -------------------------------------------------------------
682
683 // Infallibly rekey one entry, if necessary. Requires Key be rekeyable (via
684 // to HashPolicy::rekey) from Lookup.
685 void rekeyIfMoved(const Lookup& aOldValue, const Lookup& aNewValue) {
686 if (aOldValue != aNewValue) {
687 rekeyAs(aOldValue, aNewValue, aNewValue);
688 }
689 }
690
691 // Infallibly rekey one entry if present, and return whether that happened.
692 template <typename U>
693 bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup,
694 U&& aNewValue) {
695 if (Ptr p = lookup(aOldLookup)) {
696 mImpl.rekeyAndMaybeRehash(p, aNewLookup, std::forward<U>(aNewValue));
697 return true;
698 }
699 return false;
700 }
701
702 // Infallibly replace the current key at |aPtr| with an equivalent key.
703 // Specifically, both HashPolicy::hash and HashPolicy::match must return
704 // identical results for the new and old key when applied against all
705 // possible matching values.
706 template <typename U>
707 void replaceKey(Ptr aPtr, const Lookup& aLookup, U&& aNewValue) {
708 MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPtr.found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 708); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()"
")"); do { MOZ_CrashSequence(__null, 708); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
709 MOZ_ASSERT(HashPolicy::match(*aPtr, aLookup))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(HashPolicy::match(*aPtr, aLookup))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(HashPolicy::match(*aPtr, aLookup
)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("HashPolicy::match(*aPtr, aLookup)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 709); AnnotateMozCrashReason("MOZ_ASSERT" "(" "HashPolicy::match(*aPtr, aLookup)"
")"); do { MOZ_CrashSequence(__null, 709); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
710 MOZ_ASSERT(*aPtr != aNewValue)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(*aPtr != aNewValue)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(*aPtr != aNewValue))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("*aPtr != aNewValue"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 710); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*aPtr != aNewValue"
")"); do { MOZ_CrashSequence(__null, 710); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
711 const_cast<T&>(*aPtr) = std::forward<U>(aNewValue);
712 MOZ_ASSERT(*lookup(aLookup) == aNewValue)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(*lookup(aLookup) == aNewValue)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(*lookup(aLookup) == aNewValue
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"*lookup(aLookup) == aNewValue", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 712); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*lookup(aLookup) == aNewValue"
")"); do { MOZ_CrashSequence(__null, 712); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
713 }
714 void replaceKey(Ptr aPtr, const T& aNewValue) {
715 replaceKey(aPtr, aNewValue, aNewValue);
716 }
717
718 // -- Iteration ------------------------------------------------------------
719
720 // |iter()| returns an Iterator:
721 //
722 // HashSet<int> h;
723 // for (auto iter = h.iter(); !iter.done(); iter.next()) {
724 // int i = iter.get();
725 // }
726 //
727 using Iterator = typename Impl::Iterator;
728 Iterator iter() const { return mImpl.iter(); }
729
730 // |modIter()| returns a ModIterator:
731 //
732 // HashSet<int> h;
733 // for (auto iter = h.modIter(); !iter.done(); iter.next()) {
734 // if (iter.get() == 42) {
735 // iter.remove();
736 // }
737 // }
738 //
739 // Table resize may occur in ModIterator's destructor.
740 using ModIterator = typename Impl::ModIterator;
741 ModIterator modIter() { return mImpl.modIter(); }
742
743 // -- Alloc policy ---------------------------------------------------------
744
745 // Get the alloc policy.
746 const AllocPolicy& allocPolicy() const { return mImpl.allocPolicy(); }
747 AllocPolicy& allocPolicy() { return mImpl.allocPolicy(); }
748
749 // For internal use by allocation policies that provide garbage collected
750 // memory.
751 //
752 // Trace any allocations owned by this object that were made with AllocPolicy.
753 // Call the supplied closure |aTraceFunc| for each of them, passing a double
754 // pointer to the memory held (e.g. a void** pointer).
755 template <typename F>
756 void traceOwnedAllocs(F&& aTraceFunc) {
757 mImpl.traceOwnedAllocs(std::forward<F>(aTraceFunc));
758 }
759};
760
761//---------------------------------------------------------------------------
762// Hash Policy
763//---------------------------------------------------------------------------
764
765// A hash policy |HP| for a hash table with key-type |Key| must provide:
766//
767// - a type |HP::Lookup| to use to lookup table entries;
768//
769// - a static member function |HP::hash| that hashes lookup values:
770//
771// static mozilla::HashNumber hash(const Lookup&);
772//
773// - a static member function |HP::match| that tests equality of key and
774// lookup values:
775//
776// static bool match(const Key& aKey, const Lookup& aLookup);
777//
778// |aKey| and |aLookup| can have different hash numbers, only when a
779// collision happens with |prepareHash| operation, which is less frequent.
780// Thus, |HP::match| shouldn't assume the hash equality in the comparison,
781// even if the hash numbers are almost always same between them.
782//
783// Normally, Lookup = Key. In general, though, different values and types of
784// values can be used to lookup and store. If a Lookup value |l| is not equal
785// to the added Key value |k|, the user must ensure that |HP::match(k,l)| is
786// true. E.g.:
787//
788// mozilla::HashSet<Key, HP>::AddPtr p = h.lookup(l);
789// if (!p) {
790// assert(HP::match(k, l)); // must hold
791// h.add(p, k);
792// }
793
794// A pointer hashing policy that uses HashGeneric() to create good hashes for
795// pointers. Note that we don't shift out the lowest k bits because we don't
796// want to assume anything about the alignment of the pointers.
797template <typename Key>
798struct PointerHasher {
799 static_assert(std::is_pointer_v<Key>);
800
801 using Lookup = Key;
802
803 static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); }
804
805 static bool match(const Key& aKey, const Lookup& aLookup) {
806 return aKey == aLookup;
807 }
808
809 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
810};
811
812// The default hash policy, which only works with integers.
813template <class Key, typename>
814struct DefaultHasher {
815 using Lookup = Key;
816
817 static HashNumber hash(const Lookup& aLookup) {
818 // Just convert the integer to a HashNumber and use that as is. (This
819 // discards the high 32-bits of 64-bit integers!) ScrambleHashCode() is
820 // subsequently called on the value to improve the distribution.
821 return aLookup;
822 }
823
824 static bool match(const Key& aKey, const Lookup& aLookup) {
825 // Use builtin or overloaded operator==.
826 return aKey == aLookup;
827 }
828
829 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
830};
831
832// A DefaultHasher specialization for enums.
833template <class T>
834struct DefaultHasher<T, std::enable_if_t<std::is_enum_v<T>>> {
835 using Key = T;
836 using Lookup = Key;
837
838 static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); }
839
840 static bool match(const Key& aKey, const Lookup& aLookup) {
841 // Use builtin or overloaded operator==.
842 return aKey == static_cast<Key>(aLookup);
843 }
844
845 static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; }
846};
847
848// A DefaultHasher specialization for pointers.
849template <class T>
850struct DefaultHasher<T*> : PointerHasher<T*> {};
851
852// A DefaultHasher specialization for mozilla::UniquePtr.
853template <class T, class D>
854struct DefaultHasher<UniquePtr<T, D>> {
855 using Key = UniquePtr<T, D>;
856 using Lookup = Key;
857 using PtrHasher = PointerHasher<T*>;
858
859 static HashNumber hash(const Lookup& aLookup) {
860 return PtrHasher::hash(aLookup.get());
861 }
862
863 static bool match(const Key& aKey, const Lookup& aLookup) {
864 return PtrHasher::match(aKey.get(), aLookup.get());
865 }
866
867 static void rekey(Key& aKey, Key&& aNewKey) { aKey = std::move(aNewKey); }
868};
869
870// A DefaultHasher specialization for doubles.
871template <>
872struct DefaultHasher<double> {
873 using Key = double;
874 using Lookup = Key;
875
876 static HashNumber hash(const Lookup& aLookup) {
877 // Just xor the high bits with the low bits, and then treat the bits of the
878 // result as a uint32_t.
879 static_assert(sizeof(HashNumber) == 4,
880 "subsequent code assumes a four-byte hash");
881 uint64_t u = BitwiseCast<uint64_t>(aLookup);
882 return HashNumber(u ^ (u >> 32));
883 }
884
885 static bool match(const Key& aKey, const Lookup& aLookup) {
886 return BitwiseCast<uint64_t>(aKey) == BitwiseCast<uint64_t>(aLookup);
887 }
888};
889
890// A DefaultHasher specialization for floats.
891template <>
892struct DefaultHasher<float> {
893 using Key = float;
894 using Lookup = Key;
895
896 static HashNumber hash(const Lookup& aLookup) {
897 // Just use the value as if its bits form an integer. ScrambleHashCode() is
898 // subsequently called on the value to improve the distribution.
899 static_assert(sizeof(HashNumber) == 4,
900 "subsequent code assumes a four-byte hash");
901 return HashNumber(BitwiseCast<uint32_t>(aLookup));
902 }
903
904 static bool match(const Key& aKey, const Lookup& aLookup) {
905 return BitwiseCast<uint32_t>(aKey) == BitwiseCast<uint32_t>(aLookup);
906 }
907};
908
909// A hash policy for C strings.
910struct CStringHasher {
911 using Key = const char*;
912 using Lookup = const char*;
913
914 static HashNumber hash(const Lookup& aLookup) {
915 return HashString(aLookup, strlen(aLookup));
916 }
917
918 static bool match(const Key& aKey, const Lookup& aLookup) {
919 return strcmp(aKey, aLookup) == 0;
920 }
921};
922
923//---------------------------------------------------------------------------
924// Fallible Hashing Interface
925//---------------------------------------------------------------------------
926
927// Most of the time generating a hash code is infallible, but sometimes it is
928// necessary to generate hash codes on demand in a way that can fail. Specialize
929// this class for your own hash policy to provide fallible hashing.
930//
931// This is used by MovableCellHasher to handle the fact that generating a unique
932// ID for cell pointer may fail due to OOM.
933//
934// The default implementations of these methods delegate to the usual HashPolicy
935// implementation and always succeed.
936template <typename HashPolicy>
937struct FallibleHashMethods {
938 // Return true if a hashcode is already available for its argument, and
939 // sets |aHashOut|. Once this succeeds for a specific argument it
940 // must continue to do so.
941 //
942 // Return false if a hashcode is not already available. This implies that any
943 // lookup must fail, as the hash code would have to have been successfully
944 // created on insertion.
945 template <typename Lookup>
946 static bool maybeGetHash(Lookup&& aLookup, HashNumber* aHashOut) {
947 *aHashOut = HashPolicy::hash(aLookup);
948 return true;
949 }
950
951 // Fallible method to ensure a hashcode exists for its argument and create one
952 // if not. Sets |aHashOut| to the hashcode and retuns true on success. Returns
953 // false on error, e.g. out of memory.
954 template <typename Lookup>
955 static bool ensureHash(Lookup&& aLookup, HashNumber* aHashOut) {
956 *aHashOut = HashPolicy::hash(aLookup);
957 return true;
958 }
959};
960
961template <typename HashPolicy, typename Lookup>
962bool MaybeGetHash(Lookup&& aLookup, HashNumber* aHashOut) {
963 return FallibleHashMethods<typename HashPolicy::Base>::maybeGetHash(
964 std::forward<Lookup>(aLookup), aHashOut);
965}
966
967template <typename HashPolicy, typename Lookup>
968bool EnsureHash(Lookup&& aLookup, HashNumber* aHashOut) {
969 return FallibleHashMethods<typename HashPolicy::Base>::ensureHash(
970 std::forward<Lookup>(aLookup), aHashOut);
971}
972
973//---------------------------------------------------------------------------
974// Implementation Details (HashMapEntry, HashTableEntry, HashTable)
975//---------------------------------------------------------------------------
976
977// Both HashMap and HashSet are implemented by a single HashTable that is even
978// more heavily parameterized than the other two. This leaves HashTable gnarly
979// and extremely coupled to HashMap and HashSet; thus code should not use
980// HashTable directly.
981
982template <class Key, class Value>
983class HashMapEntry {
984 Key key_;
985 Value value_;
986
987 template <class, class, class>
988 friend class detail::HashTable;
989 template <class>
990 friend class detail::HashTableEntry;
991 template <class, class, class, class>
992 friend class HashMap;
993
994 public:
995 template <typename KeyInput, typename ValueInput>
996 HashMapEntry(KeyInput&& aKey, ValueInput&& aValue)
997 : key_(std::forward<KeyInput>(aKey)),
998 value_(std::forward<ValueInput>(aValue)) {}
999
1000 HashMapEntry(HashMapEntry&& aRhs) = default;
1001 HashMapEntry& operator=(HashMapEntry&& aRhs) = default;
1002
1003 using KeyType = Key;
1004 using ValueType = Value;
1005
1006 const Key& key() const { return key_; }
1007
1008 // Use this method with caution! If the key is changed such that its hash
1009 // value also changes, the map will be left in an invalid state.
1010 Key& mutableKey() { return key_; }
1011
1012 const Value& value() const { return value_; }
1013 Value& value() { return value_; }
1014
1015 static size_t offsetOfKey() { return offsetof(HashMapEntry, key_)__builtin_offsetof(HashMapEntry, key_); }
1016 static size_t offsetOfValue() { return offsetof(HashMapEntry, value_)__builtin_offsetof(HashMapEntry, value_); }
1017
1018 HashMapEntry(const HashMapEntry&) = delete;
1019 void operator=(const HashMapEntry&) = delete;
1020};
1021
1022namespace detail {
1023
1024static const HashNumber kHashTableFreeKey = 0;
1025static const HashNumber kHashTableRemovedKey = 1;
1026static const HashNumber kHashTableCollisionBit = 1;
1027
1028template <class T, class HashPolicy, class AllocPolicy>
1029class HashTable;
1030
1031template <typename T>
1032class EntrySlot;
1033
1034template <typename T>
1035class HashTableEntry {
1036 private:
1037 using NonConstT = std::remove_const_t<T>;
1038
1039 // Instead of having a hash table entry store that looks like this:
1040 //
1041 // +--------+--------+--------+--------+
1042 // | entry0 | entry1 | .... | entryN |
1043 // +--------+--------+--------+--------+
1044 //
1045 // where the entries contained their cached hash code, we're going to lay out
1046 // the entry store thusly:
1047 //
1048 // +-------+-------+-------+-------+--------+--------+--------+--------+
1049 // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN |
1050 // +-------+-------+-------+-------+--------+--------+--------+--------+
1051 //
1052 // with all the cached hashes prior to the actual entries themselves.
1053 //
1054 // We do this because implementing the first strategy requires us to make
1055 // HashTableEntry look roughly like:
1056 //
1057 // template <typename T>
1058 // class HashTableEntry {
1059 // HashNumber mKeyHash;
1060 // T mValue;
1061 // };
1062 //
1063 // The problem with this setup is that, depending on the layout of `T`, there
1064 // may be platform ABI-mandated padding between `mKeyHash` and the first
1065 // member of `T`. This ABI-mandated padding is wasted space, and can be
1066 // surprisingly common, e.g. when `T` is a single pointer on 64-bit platforms.
1067 // In such cases, we're throwing away a quarter of our entry store on padding,
1068 // which is undesirable.
1069 //
1070 // The second layout above, namely:
1071 //
1072 // +-------+-------+-------+-------+--------+--------+--------+--------+
1073 // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN |
1074 // +-------+-------+-------+-------+--------+--------+--------+--------+
1075 //
1076 // means there is no wasted space between the hashes themselves, and no wasted
1077 // space between the entries themselves. However, we would also like there to
1078 // be no gap between the last hash and the first entry. The memory allocator
1079 // guarantees the alignment of the start of the hashes. The use of a
1080 // power-of-two capacity of at least 4 guarantees that the alignment of the
1081 // *end* of the hash array is no less than the alignment of the start.
1082 // Finally, the static_asserts here guarantee that the entries themselves
1083 // don't need to be any more aligned than the alignment of the entry store
1084 // itself.
1085 //
1086 // This assertion is safe for 32-bit builds because on both Windows and Linux
1087 // (including Android), the minimum alignment for allocations larger than 8
1088 // bytes is 8 bytes, and the actual data for entries in our entry store is
1089 // guaranteed to have that alignment as well, thanks to the power-of-two
1090 // number of cached hash values stored prior to the entry data.
1091
1092 // The allocation policy must allocate a table with at least this much
1093 // alignment.
1094 static constexpr size_t kMinimumAlignment = 8;
1095
1096 static_assert(alignof(HashNumber) <= kMinimumAlignment,
1097 "[N*2 hashes, N*2 T values] allocation's alignment must be "
1098 "enough to align each hash");
1099 static_assert(alignof(NonConstT) <= 2 * sizeof(HashNumber),
1100 "subsequent N*2 T values must not require more than an even "
1101 "number of HashNumbers provides");
1102
1103 static const HashNumber sFreeKey = kHashTableFreeKey;
1104 static const HashNumber sRemovedKey = kHashTableRemovedKey;
1105 static const HashNumber sCollisionBit = kHashTableCollisionBit;
1106
1107 alignas(NonConstT) unsigned char mValueData[sizeof(NonConstT)];
1108
1109 private:
1110 template <class, class, class>
1111 friend class HashTable;
1112 template <typename>
1113 friend class EntrySlot;
1114
1115 // Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a
1116 // -Werror compile error) to reinterpret_cast<> |mValueData| to |T*|, even
1117 // through |void*|. Placing the latter cast in these separate functions
1118 // breaks the chain such that affected GCC versions no longer warn/error.
1119 void* rawValuePtr() { return mValueData; }
1120
1121 static bool isLiveHash(HashNumber hash) { return hash > sRemovedKey; }
1122
1123 NonConstT* valuePtr() { return reinterpret_cast<NonConstT*>(rawValuePtr()); }
1124
1125 void destroyStoredT() {
1126 NonConstT* ptr = valuePtr();
1127 ptr->~T();
1128 MOZ_MAKE_MEM_UNDEFINED(ptr, sizeof(*ptr))do { } while (0);
1129 }
1130
1131 public:
1132 HashTableEntry() = default;
1133
1134 ~HashTableEntry() { MOZ_MAKE_MEM_UNDEFINED(this, sizeof(*this))do { } while (0); }
1135
1136 HashTableEntry(const HashTableEntry&) = delete;
1137 void operator=(const HashTableEntry&) = delete;
1138
1139 void destroy() { destroyStoredT(); }
1140
1141 void swap(HashTableEntry* aOther, bool aOtherIsLive) {
1142 // This allows types to use Argument-Dependent-Lookup, and thus use a custom
1143 // std::swap, which is needed by types like JS::Heap and such.
1144 using std::swap;
1145
1146 if (this == aOther) {
1147 return;
1148 }
1149 if (aOtherIsLive) {
1150 swap(*valuePtr(), *aOther->valuePtr());
1151 } else {
1152 new (KnownNotNull, aOther->valuePtr()) NonConstT(std::move(*valuePtr()));
1153 destroy();
1154 }
1155 }
1156
1157 T& get() { return *valuePtr(); }
1158
1159 NonConstT& getMutable() { return *valuePtr(); }
1160};
1161
1162// A slot represents a cached hash value and its associated entry stored
1163// in the hash table. These two things are not stored in contiguous memory.
1164template <class T>
1165class EntrySlot {
1166 using NonConstT = std::remove_const_t<T>;
1167
1168 using Entry = HashTableEntry<T>;
1169
1170 Entry* mEntry;
1171 HashNumber* mKeyHash;
1172
1173 template <class, class, class>
1174 friend class HashTable;
1175
1176 EntrySlot(Entry* aEntry, HashNumber* aKeyHash)
1177 : mEntry(aEntry), mKeyHash(aKeyHash) {}
1178
1179 public:
1180 static bool isLiveHash(HashNumber hash) { return hash > Entry::sRemovedKey; }
1181
1182 EntrySlot(const EntrySlot&) = default;
1183 EntrySlot(EntrySlot&& aOther) = default;
1184
1185 EntrySlot& operator=(const EntrySlot&) = default;
1186 EntrySlot& operator=(EntrySlot&&) = default;
1187
1188 bool operator==(const EntrySlot& aRhs) const { return mEntry == aRhs.mEntry; }
1189
1190 bool operator<(const EntrySlot& aRhs) const { return mEntry < aRhs.mEntry; }
1191
1192 EntrySlot& operator++() {
1193 ++mEntry;
1194 ++mKeyHash;
1195 return *this;
1196 }
1197
1198 void destroy() { mEntry->destroy(); }
1199
1200 void swap(EntrySlot& aOther) {
1201 mEntry->swap(aOther.mEntry, aOther.isLive());
1202 std::swap(*mKeyHash, *aOther.mKeyHash);
1203 }
1204
1205 T& get() const { return mEntry->get(); }
1206
1207 NonConstT& getMutable() { return mEntry->getMutable(); }
1208
1209 bool isFree() const { return *mKeyHash == Entry::sFreeKey; }
1210
1211 void clearLive() {
1212 MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(isLive())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1212); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")"
); do { MOZ_CrashSequence(__null, 1212); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1213 *mKeyHash = Entry::sFreeKey;
1214 mEntry->destroyStoredT();
1215 }
1216
1217 void clear() {
1218 if (isLive()) {
1219 mEntry->destroyStoredT();
1220 }
1221 MOZ_MAKE_MEM_UNDEFINED(mEntry, sizeof(*mEntry))do { } while (0);
1222 *mKeyHash = Entry::sFreeKey;
1223 }
1224
1225 bool isRemoved() const { return *mKeyHash == Entry::sRemovedKey; }
1226
1227 void removeLive() {
1228 MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(isLive())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1228); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")"
); do { MOZ_CrashSequence(__null, 1228); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1229 *mKeyHash = Entry::sRemovedKey;
1230 mEntry->destroyStoredT();
1231 }
1232
1233 bool isLive() const { return isLiveHash(*mKeyHash); }
1234
1235 void setCollision() {
1236 MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(isLive())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1236); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")"
); do { MOZ_CrashSequence(__null, 1236); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1237 *mKeyHash |= Entry::sCollisionBit;
1238 }
1239 void unsetCollision() { *mKeyHash &= ~Entry::sCollisionBit; }
1240 bool hasCollision() const { return *mKeyHash & Entry::sCollisionBit; }
1241 bool matchHash(HashNumber hn) {
1242 return (*mKeyHash & ~Entry::sCollisionBit) == hn;
1243 }
1244 HashNumber getKeyHash() const { return *mKeyHash & ~Entry::sCollisionBit; }
1245
1246 template <typename... Args>
1247 void setLive(HashNumber aHashNumber, Args&&... aArgs) {
1248 MOZ_ASSERT(!isLive())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!isLive())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!isLive()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("!isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1248); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!isLive()" ")"
); do { MOZ_CrashSequence(__null, 1248); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1249 *mKeyHash = aHashNumber;
1250 new (KnownNotNull, mEntry->valuePtr()) T(std::forward<Args>(aArgs)...);
1251 MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(isLive())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1251); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")"
); do { MOZ_CrashSequence(__null, 1251); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1252 }
1253
1254 Entry* toEntry() const { return mEntry; }
1255};
1256
1257template <class T, class HashPolicy, class AllocPolicy>
1258class MOZ_STANDALONE_DEBUG[[clang::standalone_debug]] HashTable : private AllocPolicy {
1259 friend class mozilla::ReentrancyGuard;
1260
1261 using NonConstT = std::remove_const_t<T>;
1262 using Key = typename HashPolicy::KeyType;
1263 using Lookup = typename HashPolicy::Lookup;
1264
1265 public:
1266 using Entry = HashTableEntry<T>;
1267 using Slot = EntrySlot<T>;
1268
1269 template <typename F>
1270 static void forEachSlot(char* aTable, uint32_t aCapacity, F&& f) {
1271 auto hashes = reinterpret_cast<HashNumber*>(aTable);
1272 auto entries = reinterpret_cast<Entry*>(&hashes[aCapacity]);
1273 Slot slot(entries, hashes);
1274 for (size_t i = 0; i < size_t(aCapacity); ++i) {
1275 f(slot);
1276 ++slot;
1277 }
1278 }
1279
1280 // A nullable pointer to a hash table element. A Ptr |p| can be tested
1281 // either explicitly |if (p.found()) p->...| or using boolean conversion
1282 // |if (p) p->...|. Ptr objects must not be used after any mutating hash
1283 // table operations unless |generation()| is tested.
1284 class Ptr {
1285 friend class HashTable;
1286
1287 Slot mSlot;
1288#ifdef DEBUG1
1289 const HashTable* mTable;
1290 Generation mGeneration;
1291#endif
1292
1293 protected:
1294 Ptr(Slot aSlot, const HashTable& aTable)
1295 : mSlot(aSlot)
1296#ifdef DEBUG1
1297 ,
1298 mTable(&aTable),
1299 mGeneration(aTable.generation())
1300#endif
1301 {
1302 }
1303
1304 // This constructor is used only by AddPtr() within lookupForAdd().
1305 explicit Ptr(const HashTable& aTable)
1306 : mSlot(nullptr, nullptr)
1307#ifdef DEBUG1
1308 ,
1309 mTable(&aTable),
1310 mGeneration(aTable.generation())
1311#endif
1312 {
1313 }
1314
1315 bool isValid() const { return !!mSlot.toEntry(); }
1316
1317 public:
1318 Ptr()
1319 : mSlot(nullptr, nullptr)
1320#ifdef DEBUG1
1321 ,
1322 mTable(nullptr),
9
Null pointer value stored to 'ptr.mTable'
1323 mGeneration(0)
1324#endif
1325 {
1326 }
1327
1328 bool found() const {
1329 if (!isValid()) {
1330 return false;
1331 }
1332#ifdef DEBUG1
1333 MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mGeneration == mTable->generation())>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(mGeneration == mTable->generation()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1333); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()"
")"); do { MOZ_CrashSequence(__null, 1333); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1334#endif
1335 return mSlot.isLive();
1336 }
1337
1338 explicit operator bool() const { return found(); }
1339
1340 bool operator==(const Ptr& aRhs) const {
1341 MOZ_ASSERT(found() && aRhs.found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(found() && aRhs.found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(found() && aRhs.found
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("found() && aRhs.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1341); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found() && aRhs.found()"
")"); do { MOZ_CrashSequence(__null, 1341); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1342 return mSlot == aRhs.mSlot;
1343 }
1344
1345 bool operator!=(const Ptr& aRhs) const {
1346#ifdef DEBUG1
1347 MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mGeneration == mTable->generation())>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(mGeneration == mTable->generation()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1347); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()"
")"); do { MOZ_CrashSequence(__null, 1347); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1348#endif
1349 return !(*this == aRhs);
1350 }
1351
1352 T& operator*() const {
1353#ifdef DEBUG1
1354 MOZ_ASSERT(found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(found()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1354); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")"
); do { MOZ_CrashSequence(__null, 1354); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1355 MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mGeneration == mTable->generation())>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(mGeneration == mTable->generation()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1355); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()"
")"); do { MOZ_CrashSequence(__null, 1355); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1356#endif
1357 return mSlot.get();
1358 }
1359
1360 T* operator->() const {
1361#ifdef DEBUG1
1362 MOZ_ASSERT(found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(found()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1362); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")"
); do { MOZ_CrashSequence(__null, 1362); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
17
Assuming the condition is false
18
Taking false branch
19
Loop condition is false. Exiting loop
1363 MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mGeneration == mTable->generation())>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(mGeneration == mTable->generation()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1363); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()"
")"); do { MOZ_CrashSequence(__null, 1363); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
20
Called C++ object pointer is null
1364#endif
1365 return &mSlot.get();
1366 }
1367 };
1368
1369 // A Ptr that can be used to add a key after a failed lookup.
1370 class AddPtr : public Ptr {
1371 friend class HashTable;
1372
1373 HashNumber mKeyHash;
1374#ifdef DEBUG1
1375 uint64_t mMutationCount;
1376#endif
1377
1378 AddPtr(Slot aSlot, const HashTable& aTable, HashNumber aHashNumber)
1379 : Ptr(aSlot, aTable),
1380 mKeyHash(aHashNumber)
1381#ifdef DEBUG1
1382 ,
1383 mMutationCount(aTable.mMutationCount)
1384#endif
1385 {
1386 }
1387
1388 // This constructor is used when lookupForAdd() is performed on a table
1389 // lacking entry storage; it leaves mSlot null but initializes everything
1390 // else.
1391 AddPtr(const HashTable& aTable, HashNumber aHashNumber)
1392 : Ptr(aTable),
1393 mKeyHash(aHashNumber)
1394#ifdef DEBUG1
1395 ,
1396 mMutationCount(aTable.mMutationCount)
1397#endif
1398 {
1399 MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(isLive())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1399); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")"
); do { MOZ_CrashSequence(__null, 1399); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1400 }
1401
1402 bool isLive() const { return isLiveHash(mKeyHash); }
1403
1404 public:
1405 AddPtr() : mKeyHash(0) {}
1406 };
1407
1408 // A hash table iterator that (mostly) doesn't allow table modifications.
1409 // As with Ptr/AddPtr, Iterator objects must not be used after any mutating
1410 // hash table operation unless the |generation()| is tested.
1411 class Iterator {
1412 void moveToNextLiveEntry() {
1413 while (++mCur < mEnd && !mCur.isLive()) {
1414 continue;
1415 }
1416 }
1417
1418 protected:
1419 friend class HashTable;
1420
1421 explicit Iterator(const HashTable& aTable)
1422 : mCur(aTable.slotForIndex(0)),
1423 mEnd(aTable.slotForIndex(aTable.capacity()))
1424#ifdef DEBUG1
1425 ,
1426 mTable(aTable),
1427 mMutationCount(aTable.mMutationCount),
1428 mGeneration(aTable.generation()),
1429 mValidEntry(true)
1430#endif
1431 {
1432 if (!done() && !mCur.isLive()) {
1433 moveToNextLiveEntry();
1434 }
1435 }
1436
1437 Slot mCur;
1438 Slot mEnd;
1439#ifdef DEBUG1
1440 const HashTable& mTable;
1441 uint64_t mMutationCount;
1442 Generation mGeneration;
1443 bool mValidEntry;
1444#endif
1445
1446 public:
1447 bool done() const {
1448 MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("mGeneration == mTable.generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1448); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()"
")"); do { MOZ_CrashSequence(__null, 1448); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1449 MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mMutationCount == mTable.mMutationCount)>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1449); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount"
")"); do { MOZ_CrashSequence(__null, 1449); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1450 return mCur == mEnd;
1451 }
1452
1453 T& get() const {
1454 MOZ_ASSERT(!done())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!done())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!done()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("!done()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1454); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")"
); do { MOZ_CrashSequence(__null, 1454); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1455 MOZ_ASSERT(mValidEntry)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mValidEntry)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mValidEntry))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("mValidEntry", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1455); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mValidEntry"
")"); do { MOZ_CrashSequence(__null, 1455); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1456 MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("mGeneration == mTable.generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1456); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()"
")"); do { MOZ_CrashSequence(__null, 1456); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1457 MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mMutationCount == mTable.mMutationCount)>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1457); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount"
")"); do { MOZ_CrashSequence(__null, 1457); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1458 return mCur.get();
1459 }
1460
1461 void next() {
1462 MOZ_ASSERT(!done())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!done())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!done()))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("!done()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1462); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")"
); do { MOZ_CrashSequence(__null, 1462); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1463 MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("mGeneration == mTable.generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1463); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()"
")"); do { MOZ_CrashSequence(__null, 1463); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1464 MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mMutationCount == mTable.mMutationCount)>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1464); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount"
")"); do { MOZ_CrashSequence(__null, 1464); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1465 moveToNextLiveEntry();
1466#ifdef DEBUG1
1467 mValidEntry = true;
1468#endif
1469 }
1470 };
1471
1472 // A hash table iterator that permits modification, removal and rekeying.
1473 // Since rehashing when elements were removed during iteration would be
1474 // bad, it is postponed until the ModIterator is destructed. Since the
1475 // ModIterator's destructor touches the hash table, the user must ensure
1476 // that the hash table is still alive when the destructor runs.
1477 class ModIterator : public Iterator {
1478 friend class HashTable;
1479
1480 HashTable& mTable;
1481 bool mRekeyed;
1482 bool mRemoved;
1483
1484 protected:
1485 explicit ModIterator(HashTable& aTable)
1486 : Iterator(aTable), mTable(aTable), mRekeyed(false), mRemoved(false) {}
1487
1488 public:
1489 MOZ_IMPLICIT ModIterator(ModIterator&& aOther)
1490 : Iterator(aOther),
1491 mTable(aOther.mTable),
1492 mRekeyed(aOther.mRekeyed),
1493 mRemoved(aOther.mRemoved) {
1494 aOther.mRekeyed = false;
1495 aOther.mRemoved = false;
1496 }
1497
1498 // ModIterator is movable but not copyable.
1499 ModIterator(const ModIterator&) = delete;
1500 void operator=(const ModIterator&) = delete;
1501
1502 // Removes the current element from the table, leaving |get()| invalid until
1503 // the next call to |next()|.
1504 //
1505 // See the comments on ~ModIterator about table resizing after removing
1506 // entries.
1507 void remove() {
1508 mTable.remove(this->mCur);
1509 mRemoved = true;
1510#ifdef DEBUG1
1511 this->mValidEntry = false;
1512 this->mMutationCount = mTable.mMutationCount;
1513#endif
1514 }
1515
1516 NonConstT& getMutable() {
1517 MOZ_ASSERT(!this->done())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!this->done())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!this->done()))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("!this->done()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1517); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!this->done()"
")"); do { MOZ_CrashSequence(__null, 1517); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1518 MOZ_ASSERT(this->mValidEntry)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(this->mValidEntry)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(this->mValidEntry))), 0))
) { do { } while (false); MOZ_ReportAssertionFailure("this->mValidEntry"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1518); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mValidEntry"
")"); do { MOZ_CrashSequence(__null, 1518); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1519 MOZ_ASSERT(this->mGeneration == this->Iterator::mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(this->mGeneration == this->Iterator::mTable.generation
())>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(this->mGeneration == this->Iterator::mTable.generation
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("this->mGeneration == this->Iterator::mTable.generation()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1519); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mGeneration == this->Iterator::mTable.generation()"
")"); do { MOZ_CrashSequence(__null, 1519); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1520 MOZ_ASSERT(this->mMutationCount == this->Iterator::mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(this->mMutationCount == this->Iterator::mTable
.mMutationCount)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(this->mMutationCount == this
->Iterator::mTable.mMutationCount))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("this->mMutationCount == this->Iterator::mTable.mMutationCount"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1520); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mMutationCount == this->Iterator::mTable.mMutationCount"
")"); do { MOZ_CrashSequence(__null, 1520); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1521 return this->mCur.getMutable();
1522 }
1523
1524 // Removes the current element and re-inserts it into the table with
1525 // a new key at the new Lookup position. |get()| is invalid after
1526 // this operation until the next call to |next()|.
1527 template <typename KeyInput>
1528 void rekey(const Lookup& l, KeyInput&& k) {
1529 MOZ_ASSERT(do { static_assert( mozilla::detail::AssertionConditionType<
decltype(static_cast<const void*>(&k) != static_cast
<const void*>(&HashPolicy::getKey(this->mCur.get
())))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(static_cast<const void*>(&k) != static_cast
<const void*>(&HashPolicy::getKey(this->mCur.get
()))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))"
" (" "Don't pass a reference into the table here" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1532); AnnotateMozCrashReason("MOZ_ASSERT" "(" "static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))"
") (" "Don't pass a reference into the table here" ")"); do {
MOZ_CrashSequence(__null, 1532); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
1530 static_cast<const void*>(&k) !=do { static_assert( mozilla::detail::AssertionConditionType<
decltype(static_cast<const void*>(&k) != static_cast
<const void*>(&HashPolicy::getKey(this->mCur.get
())))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(static_cast<const void*>(&k) != static_cast
<const void*>(&HashPolicy::getKey(this->mCur.get
()))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))"
" (" "Don't pass a reference into the table here" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1532); AnnotateMozCrashReason("MOZ_ASSERT" "(" "static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))"
") (" "Don't pass a reference into the table here" ")"); do {
MOZ_CrashSequence(__null, 1532); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
1531 static_cast<const void*>(&HashPolicy::getKey(this->mCur.get())),do { static_assert( mozilla::detail::AssertionConditionType<
decltype(static_cast<const void*>(&k) != static_cast
<const void*>(&HashPolicy::getKey(this->mCur.get
())))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(static_cast<const void*>(&k) != static_cast
<const void*>(&HashPolicy::getKey(this->mCur.get
()))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))"
" (" "Don't pass a reference into the table here" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1532); AnnotateMozCrashReason("MOZ_ASSERT" "(" "static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))"
") (" "Don't pass a reference into the table here" ")"); do {
MOZ_CrashSequence(__null, 1532); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
1532 "Don't pass a reference into the table here")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(static_cast<const void*>(&k) != static_cast
<const void*>(&HashPolicy::getKey(this->mCur.get
())))>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(static_cast<const void*>(&k) != static_cast
<const void*>(&HashPolicy::getKey(this->mCur.get
()))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))"
" (" "Don't pass a reference into the table here" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1532); AnnotateMozCrashReason("MOZ_ASSERT" "(" "static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))"
") (" "Don't pass a reference into the table here" ")"); do {
MOZ_CrashSequence(__null, 1532); __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
1533 Ptr p(this->mCur, mTable);
1534 mTable.rekeyWithoutRehash(p, l, std::forward<KeyInput>(k));
1535 mRekeyed = true;
1536#ifdef DEBUG1
1537 this->mValidEntry = false;
1538 this->mMutationCount = mTable.mMutationCount;
1539#endif
1540 }
1541
1542 void rekey(const Lookup& l) { rekey(l, l); }
1543
1544 // This can rehash the table or resize it if entries were removed.
1545 //
1546 // This does not go as far as freeing the table if it is now empty, as that
1547 // can lead to repeatedly allocating and freeing the table when a small
1548 // number of entries are repeatedly added and removed. If callers require
1549 // memory to be minimised after removing entries they should call compact().
1550 ~ModIterator() {
1551 if (mRekeyed) {
1552 mTable.incrementGeneration();
1553 mTable.infallibleRehashIfOverloaded();
1554 }
1555
1556 if (mRemoved) {
1557 mTable.shrinkToBestCapacity();
1558 }
1559 }
1560 };
1561
1562 // HashTable is movable
1563 HashTable(HashTable&& aRhs) : AllocPolicy(std::move(aRhs)) { moveFrom(aRhs); }
1564 HashTable& operator=(HashTable&& aRhs) {
1565 MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(this != &aRhs)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(this != &aRhs))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("this != &aRhs"
" (" "self-move assignment is prohibited" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1565); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &aRhs"
") (" "self-move assignment is prohibited" ")"); do { MOZ_CrashSequence
(__null, 1565); __attribute__((nomerge)) ::abort(); } while (
false); } } while (false)
;
1566 if (mTable) {
1567 destroyTable(*this, mTable, capacity());
1568 }
1569 AllocPolicy::operator=(std::move(aRhs));
1570 moveFrom(aRhs);
1571 return *this;
1572 }
1573
1574 void swap(HashTable& aOther) {
1575 ReentrancyGuard g1(*this);
1576 ReentrancyGuard g2(aOther);
1577
1578 std::swap(mGenAndHashShift, aOther.mGenAndHashShift);
1579 std::swap(mTable, aOther.mTable);
1580 std::swap(mEntryCount, aOther.mEntryCount);
1581 std::swap(mRemovedCount, aOther.mRemovedCount);
1582#ifdef DEBUG1
1583 std::swap(mMutationCount, aOther.mMutationCount);
1584 std::swap(mEntered, aOther.mEntered);
1585#endif
1586 }
1587
1588 AllocPolicy& allocPolicy() { return *this; }
1589 const AllocPolicy& allocPolicy() const { return *this; }
1590
1591 template <typename F>
1592 void traceOwnedAllocs(F&& aTraceFunc) {
1593 if (mTable) {
1594 aTraceFunc(&mTable);
1595 }
1596 }
1597
1598 private:
1599 void moveFrom(HashTable& aRhs) {
1600 mGenAndHashShift = aRhs.mGenAndHashShift;
1601 mTable = aRhs.mTable;
1602 mEntryCount = aRhs.mEntryCount;
1603 mRemovedCount = aRhs.mRemovedCount;
1604#ifdef DEBUG1
1605 mMutationCount = aRhs.mMutationCount;
1606 mEntered = aRhs.mEntered;
1607#endif
1608 aRhs.mTable = nullptr;
1609 aRhs.clearAndCompact();
1610 }
1611
1612 static const uint32_t CAP_BITS = 30;
1613
1614 public:
1615 uint64_t mGenAndHashShift; // entry storage generation number (56 bits)
1616 // and multiplicative hash shift (8 bits)
1617 char* mTable; // entry storage
1618 uint32_t mEntryCount; // number of entries in mTable
1619 uint32_t mRemovedCount; // removed entry sentinels in mTable
1620
1621#ifdef DEBUG1
1622 uint64_t mMutationCount;
1623 mutable bool mEntered;
1624#endif
1625
1626 // The default initial capacity is 32 (enough to hold 16 elements), but it
1627 // can be as low as 4.
1628 static const uint32_t sDefaultLen = 16;
1629 static const uint32_t sMinCapacity = 4;
1630 // See the comments in HashTableEntry about this value.
1631 static_assert(sMinCapacity >= 4, "too-small sMinCapacity breaks assumptions");
1632 static const uint32_t sMaxInit = 1u << (CAP_BITS - 1);
1633 static const uint32_t sMaxCapacity = 1u << CAP_BITS;
1634
1635 // Hash-table alpha is conceptually a fraction, but to avoid floating-point
1636 // math we implement it as a ratio of integers.
1637 static const uint8_t sAlphaDenominator = 4;
1638 static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4
1639 static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4
1640
1641 static const HashNumber sFreeKey = Entry::sFreeKey;
1642 static const HashNumber sRemovedKey = Entry::sRemovedKey;
1643 static const HashNumber sCollisionBit = Entry::sCollisionBit;
1644
1645 static const uint64_t sHashShiftBits = 8;
1646 static const uint64_t sHashShiftMask = (1 << sHashShiftBits) - 1;
1647 static const uint64_t sGenerationShift = sHashShiftBits;
1648
1649 MOZ_ALWAYS_INLINEinline uint8_t hashShift() const {
1650 return uint8_t(mGenAndHashShift & sHashShiftMask);
1651 }
1652 MOZ_ALWAYS_INLINEinline uint64_t gen() const {
1653 return mGenAndHashShift >> sGenerationShift;
1654 }
1655
1656 // HashTable is not copyable or assignable
1657 HashTable(const HashTable&) = delete;
1658 void operator=(const HashTable&) = delete;
1659
1660 private:
1661 void setGenAndHashShift(uint64_t aGeneration, uint8_t aHashShift) {
1662 mGenAndHashShift = aGeneration << sGenerationShift | aHashShift;
1663 }
1664
1665 public:
1666 void incrementGeneration() { setGenAndHashShift(gen() + 1, hashShift()); }
1667 void setHashShift(uint32_t aHashShift) {
1668 MOZ_ASSERT((aHashShift & sHashShiftMask) == aHashShift)do { static_assert( mozilla::detail::AssertionConditionType<
decltype((aHashShift & sHashShiftMask) == aHashShift)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!((aHashShift & sHashShiftMask) == aHashShift))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("(aHashShift & sHashShiftMask) == aHashShift"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1668); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(aHashShift & sHashShiftMask) == aHashShift"
")"); do { MOZ_CrashSequence(__null, 1668); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1669 mGenAndHashShift = (mGenAndHashShift & ~sHashShiftMask) | aHashShift;
1670 }
1671
1672 constexpr static uint32_t bestCapacity(uint32_t aLen) {
1673 static_assert(
1674 (sMaxInit * sAlphaDenominator) / sAlphaDenominator == sMaxInit,
1675 "multiplication in numerator below could overflow");
1676 static_assert(
1677 sMaxInit * sAlphaDenominator <= UINT32_MAX(4294967295U) - sMaxAlphaNumerator,
1678 "numerator calculation below could potentially overflow");
1679
1680 // Callers should ensure this is true.
1681 MOZ_ASSERT(aLen <= sMaxInit)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aLen <= sMaxInit)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aLen <= sMaxInit))), 0)))
{ do { } while (false); MOZ_ReportAssertionFailure("aLen <= sMaxInit"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1681); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aLen <= sMaxInit"
")"); do { MOZ_CrashSequence(__null, 1681); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1682
1683 // Compute the smallest capacity allowing |aLen| elements to be
1684 // inserted without rehashing: ceil(aLen / max-alpha). (Ceiling
1685 // integral division: <http://stackoverflow.com/a/2745086>.)
1686 uint32_t capacity = (aLen * sAlphaDenominator + sMaxAlphaNumerator - 1) /
1687 sMaxAlphaNumerator;
1688 capacity = (capacity < sMinCapacity) ? sMinCapacity : RoundUpPow2(capacity);
1689
1690 MOZ_ASSERT(capacity >= aLen)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(capacity >= aLen)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(capacity >= aLen))), 0)))
{ do { } while (false); MOZ_ReportAssertionFailure("capacity >= aLen"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1690); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity >= aLen"
")"); do { MOZ_CrashSequence(__null, 1690); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1691 MOZ_ASSERT(capacity <= sMaxCapacity)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(capacity <= sMaxCapacity)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(capacity <= sMaxCapacity)
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("capacity <= sMaxCapacity"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1691); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity <= sMaxCapacity"
")"); do { MOZ_CrashSequence(__null, 1691); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1692
1693 return capacity;
1694 }
1695
1696 constexpr static uint32_t hashShiftForLength(uint32_t aLen) {
1697 // Reject all lengths whose initial computed capacity would exceed
1698 // sMaxCapacity. Round that maximum aLen down to the nearest power of two
1699 // for speedier code.
1700 if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) {
1701 MOZ_CRASH("initial length is too large")do { do { } while (false); MOZ_ReportCrash("" "initial length is too large"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1701); AnnotateMozCrashReason("MOZ_CRASH(" "initial length is too large"
")"); do { MOZ_CrashSequence(__null, 1701); __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
1702 }
1703
1704 return kHashNumberBits - mozilla::CeilingLog2(bestCapacity(aLen));
1705 }
1706
1707 static bool isLiveHash(HashNumber aHash) { return Entry::isLiveHash(aHash); }
1708
1709 static HashNumber prepareHash(HashNumber aInputHash) {
1710 HashNumber keyHash = ScrambleHashCode(aInputHash);
1711
1712 // Avoid reserved hash codes.
1713 if (!isLiveHash(keyHash)) {
1714 keyHash -= (sRemovedKey + 1);
1715 }
1716 return keyHash & ~sCollisionBit;
1717 }
1718
1719 enum FailureBehavior { DontReportFailure = false, ReportFailure = true };
1720
1721 // Fake a struct that we're going to alloc. See the comments in
1722 // HashTableEntry about how the table is laid out, and why it's safe.
1723 struct FakeSlot {
1724 unsigned char c[sizeof(HashNumber) + sizeof(typename Entry::NonConstT)];
1725 };
1726
1727 static char* createTable(AllocPolicy& aAllocPolicy, uint32_t aCapacity,
1728 FailureBehavior aReportFailure = ReportFailure) {
1729 FakeSlot* fake =
1730 aReportFailure
1731 ? aAllocPolicy.template pod_malloc<FakeSlot>(aCapacity)
1732 : aAllocPolicy.template maybe_pod_malloc<FakeSlot>(aCapacity);
1733
1734 MOZ_ASSERT((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) ==do { static_assert( mozilla::detail::AssertionConditionType<
decltype((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment
) == 0)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment
) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1735); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0"
")"); do { MOZ_CrashSequence(__null, 1735); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
1735 0)do { static_assert( mozilla::detail::AssertionConditionType<
decltype((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment
) == 0)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment
) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1735); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0"
")"); do { MOZ_CrashSequence(__null, 1735); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1736
1737 char* table = reinterpret_cast<char*>(fake);
1738 if (table) {
1739 forEachSlot(table, aCapacity, [&](Slot& slot) {
1740 *slot.mKeyHash = sFreeKey;
1741 new (KnownNotNull, slot.toEntry()) Entry();
1742 });
1743 }
1744 return table;
1745 }
1746
1747 static void destroyTable(AllocPolicy& aAllocPolicy, char* aOldTable,
1748 uint32_t aCapacity) {
1749 forEachSlot(aOldTable, aCapacity, [&](const Slot& slot) {
1750 if (slot.isLive()) {
1751 slot.toEntry()->destroyStoredT();
1752 }
1753 });
1754 freeTable(aAllocPolicy, aOldTable, aCapacity);
1755 }
1756
1757 static void freeTable(AllocPolicy& aAllocPolicy, char* aOldTable,
1758 uint32_t aCapacity) {
1759 FakeSlot* fake = reinterpret_cast<FakeSlot*>(aOldTable);
1760 aAllocPolicy.free_(fake, aCapacity);
1761 }
1762
1763 public:
1764 constexpr HashTable(AllocPolicy aAllocPolicy, uint32_t aLen)
1765 : AllocPolicy(std::move(aAllocPolicy)),
1766 mGenAndHashShift(hashShiftForLength(aLen)),
1767 mTable(nullptr),
1768 mEntryCount(0),
1769 mRemovedCount(0)
1770#ifdef DEBUG1
1771 ,
1772 mMutationCount(0),
1773 mEntered(false)
1774#endif
1775 {
1776 }
1777
1778 explicit HashTable(AllocPolicy aAllocPolicy)
1779 : HashTable(aAllocPolicy, sDefaultLen) {}
1780
1781 ~HashTable() {
1782 if (mTable) {
1783 destroyTable(*this, mTable, capacity());
1784 }
1785 }
1786
1787 private:
1788 HashNumber hash1(HashNumber aHash0) const { return aHash0 >> hashShift(); }
1789
1790 struct DoubleHash {
1791 HashNumber mHash2;
1792 HashNumber mSizeMask;
1793 };
1794
1795 DoubleHash hash2(HashNumber aCurKeyHash) const {
1796 uint32_t sizeLog2 = kHashNumberBits - hashShift();
1797 DoubleHash dh = {((aCurKeyHash << sizeLog2) >> hashShift()) | 1,
1798 (HashNumber(1) << sizeLog2) - 1};
1799 return dh;
1800 }
1801
1802 static HashNumber applyDoubleHash(HashNumber aHash1,
1803 const DoubleHash& aDoubleHash) {
1804 return WrappingSubtract(aHash1, aDoubleHash.mHash2) & aDoubleHash.mSizeMask;
1805 }
1806
1807 static MOZ_ALWAYS_INLINEinline bool match(T& aEntry, const Lookup& aLookup) {
1808 return HashPolicy::match(HashPolicy::getKey(aEntry), aLookup);
1809 }
1810
1811 enum LookupReason { ForNonAdd, ForAdd };
1812
1813 Slot slotForIndex(HashNumber aIndex) const {
1814 auto hashes = reinterpret_cast<HashNumber*>(mTable);
1815 auto entries = reinterpret_cast<Entry*>(&hashes[capacity()]);
1816 return Slot(&entries[aIndex], &hashes[aIndex]);
1817 }
1818
1819 // Warning: in order for readonlyThreadsafeLookup() to be safe this
1820 // function must not modify the table in any way when Reason==ForNonAdd.
1821 template <LookupReason Reason>
1822 MOZ_ALWAYS_INLINEinline Slot lookup(const Lookup& aLookup,
1823 HashNumber aKeyHash) const {
1824 MOZ_ASSERT(isLiveHash(aKeyHash))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(isLiveHash(aKeyHash))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(isLiveHash(aKeyHash)))), 0))
) { do { } while (false); MOZ_ReportAssertionFailure("isLiveHash(aKeyHash)"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1824); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLiveHash(aKeyHash)"
")"); do { MOZ_CrashSequence(__null, 1824); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1825 MOZ_ASSERT(!(aKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!(aKeyHash & sCollisionBit))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!(aKeyHash & sCollisionBit
)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!(aKeyHash & sCollisionBit)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1825); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)"
")"); do { MOZ_CrashSequence(__null, 1825); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1826 MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mTable)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1826); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")"
); do { MOZ_CrashSequence(__null, 1826); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1827
1828 // Compute the primary hash address.
1829 HashNumber h1 = hash1(aKeyHash);
1830 Slot slot = slotForIndex(h1);
1831
1832 // Miss: return space for a new entry.
1833 if (slot.isFree()) {
1834 return slot;
1835 }
1836
1837 // Hit: return entry.
1838 if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) {
1839 return slot;
1840 }
1841
1842 // Collision: double hash.
1843 DoubleHash dh = hash2(aKeyHash);
1844
1845 // Save the first removed entry pointer so we can recycle later.
1846 Maybe<Slot> firstRemoved;
1847
1848 while (true) {
1849 if (Reason == ForAdd && !firstRemoved) {
1850 if (MOZ_UNLIKELY(slot.isRemoved())(__builtin_expect(!!(slot.isRemoved()), 0))) {
1851 firstRemoved.emplace(slot);
1852 } else {
1853 slot.setCollision();
1854 }
1855 }
1856
1857 h1 = applyDoubleHash(h1, dh);
1858
1859 slot = slotForIndex(h1);
1860 if (slot.isFree()) {
1861 return firstRemoved.refOr(slot);
1862 }
1863
1864 if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) {
1865 return slot;
1866 }
1867 }
1868 }
1869
1870 // This is a copy of lookup() hardcoded to the assumptions:
1871 // 1. the lookup is for an add;
1872 // 2. the key, whose |keyHash| has been passed, is not in the table.
1873 Slot findNonLiveSlot(HashNumber aKeyHash) {
1874 MOZ_ASSERT(!(aKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!(aKeyHash & sCollisionBit))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!(aKeyHash & sCollisionBit
)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!(aKeyHash & sCollisionBit)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1874); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)"
")"); do { MOZ_CrashSequence(__null, 1874); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1875 MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mTable)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1875); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")"
); do { MOZ_CrashSequence(__null, 1875); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1876
1877 // We assume 'aKeyHash' has already been distributed.
1878
1879 // Compute the primary hash address.
1880 HashNumber h1 = hash1(aKeyHash);
1881 Slot slot = slotForIndex(h1);
1882
1883 // Miss: return space for a new entry.
1884 if (!slot.isLive()) {
1885 return slot;
1886 }
1887
1888 // Collision: double hash.
1889 DoubleHash dh = hash2(aKeyHash);
1890
1891 while (true) {
1892 slot.setCollision();
1893
1894 h1 = applyDoubleHash(h1, dh);
1895
1896 slot = slotForIndex(h1);
1897 if (!slot.isLive()) {
1898 return slot;
1899 }
1900 }
1901 }
1902
1903 enum RebuildStatus { NotOverloaded, Rehashed, RehashFailed };
1904
1905 RebuildStatus changeTableSize(
1906 uint32_t newCapacity, FailureBehavior aReportFailure = ReportFailure) {
1907 MOZ_ASSERT(std::has_single_bit(newCapacity))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(std::has_single_bit(newCapacity))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(std::has_single_bit(newCapacity
)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("std::has_single_bit(newCapacity)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1907); AnnotateMozCrashReason("MOZ_ASSERT" "(" "std::has_single_bit(newCapacity)"
")"); do { MOZ_CrashSequence(__null, 1907); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1908 MOZ_ASSERT(!!mTable == !!capacity())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!!mTable == !!capacity())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!!mTable == !!capacity()))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("!!mTable == !!capacity()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1908); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!!mTable == !!capacity()"
")"); do { MOZ_CrashSequence(__null, 1908); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1909
1910 // Look, but don't touch, until we succeed in getting new entry store.
1911 char* oldTable = mTable;
1912 uint32_t oldCapacity = capacity();
1913 uint32_t newLog2 = mozilla::CeilingLog2(newCapacity);
1914
1915 if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)(__builtin_expect(!!(newCapacity > sMaxCapacity), 0))) {
1916 if (aReportFailure) {
1917 this->reportAllocOverflow();
1918 }
1919 return RehashFailed;
1920 }
1921
1922 char* newTable = createTable(*this, newCapacity, aReportFailure);
1923 if (!newTable) {
1924 return RehashFailed;
1925 }
1926
1927 // We can't fail from here on, so update table parameters.
1928 mRemovedCount = 0;
1929 incrementGeneration();
1930 setHashShift(kHashNumberBits - newLog2);
1931 mTable = newTable;
1932
1933 // Copy only live entries, leaving removed ones behind.
1934 forEachSlot(oldTable, oldCapacity, [&](Slot& slot) {
1935 if (slot.isLive()) {
1936 HashNumber hn = slot.getKeyHash();
1937 findNonLiveSlot(hn).setLive(
1938 hn, std::move(const_cast<typename Entry::NonConstT&>(slot.get())));
1939 }
1940
1941 slot.clear();
1942 });
1943
1944 // All entries have been destroyed, no need to destroyTable.
1945 freeTable(*this, oldTable, oldCapacity);
1946 return Rehashed;
1947 }
1948
1949 RebuildStatus rehashIfOverloaded(
1950 FailureBehavior aReportFailure = ReportFailure) {
1951 static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMaxAlphaNumerator,
1952 "multiplication below could overflow");
1953
1954 // Note: if capacity() is zero, this will always succeed, which is
1955 // what we want.
1956 bool overloaded = mEntryCount + mRemovedCount >=
1957 capacity() * sMaxAlphaNumerator / sAlphaDenominator;
1958
1959 if (!overloaded) {
1960 return NotOverloaded;
1961 }
1962
1963 // Succeed if a quarter or more of all entries are removed. Note that this
1964 // always succeeds if capacity() == 0 (i.e. entry storage has not been
1965 // allocated), which is what we want, because it means changeTableSize()
1966 // will allocate the requested capacity rather than doubling it.
1967 bool manyRemoved = mRemovedCount >= (capacity() >> 2);
1968 uint32_t newCapacity = manyRemoved ? rawCapacity() : rawCapacity() * 2;
1969 return changeTableSize(newCapacity, aReportFailure);
1970 }
1971
1972 void infallibleRehashIfOverloaded() {
1973 if (rehashIfOverloaded(DontReportFailure) == RehashFailed) {
1974 rehashTableInPlace();
1975 }
1976 }
1977
1978 void remove(Slot& aSlot) {
1979 MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mTable)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 1979); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")"
); do { MOZ_CrashSequence(__null, 1979); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1980
1981 if (aSlot.hasCollision()) {
1982 aSlot.removeLive();
1983 mRemovedCount++;
1984 } else {
1985 aSlot.clearLive();
1986 }
1987 mEntryCount--;
1988#ifdef DEBUG1
1989 mMutationCount++;
1990#endif
1991 }
1992
1993 void shrinkIfUnderloaded() {
1994 static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMinAlphaNumerator,
1995 "multiplication below could overflow");
1996 bool underloaded =
1997 capacity() > sMinCapacity &&
1998 mEntryCount <= capacity() * sMinAlphaNumerator / sAlphaDenominator;
1999
2000 if (underloaded) {
2001 (void)changeTableSize(capacity() / 2, DontReportFailure);
2002 }
2003 }
2004
2005 // This is identical to changeTableSize(currentSize), but without requiring
2006 // a second table. We do this by recycling the collision bits to tell us if
2007 // the element is already inserted or still waiting to be inserted. Since
2008 // already-inserted elements win any conflicts, we get the same table as we
2009 // would have gotten through random insertion order.
2010 void rehashTableInPlace() {
2011 mRemovedCount = 0;
2012 incrementGeneration();
2013 forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.unsetCollision(); });
2014 for (uint32_t i = 0; i < capacity();) {
2015 Slot src = slotForIndex(i);
2016
2017 if (!src.isLive() || src.hasCollision()) {
2018 ++i;
2019 continue;
2020 }
2021
2022 HashNumber keyHash = src.getKeyHash();
2023 HashNumber h1 = hash1(keyHash);
2024 DoubleHash dh = hash2(keyHash);
2025 Slot tgt = slotForIndex(h1);
2026 while (true) {
2027 if (!tgt.hasCollision()) {
2028 src.swap(tgt);
2029 tgt.setCollision();
2030 break;
2031 }
2032
2033 h1 = applyDoubleHash(h1, dh);
2034 tgt = slotForIndex(h1);
2035 }
2036 }
2037
2038 // TODO: this algorithm leaves collision bits on *all* elements, even if
2039 // they are on no collision path. We have the option of setting the
2040 // collision bits correctly on a subsequent pass or skipping the rehash
2041 // unless we are totally filled with tombstones: benchmark to find out
2042 // which approach is best.
2043 }
2044
2045 // Prefer to use putNewInfallible; this function does not check
2046 // invariants.
2047 template <typename... Args>
2048 void putNewInfallibleInternal(HashNumber aKeyHash, Args&&... aArgs) {
2049 MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mTable)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2049); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")"
); do { MOZ_CrashSequence(__null, 2049); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2050
2051 Slot slot = findNonLiveSlot(aKeyHash);
2052
2053 if (slot.isRemoved()) {
2054 mRemovedCount--;
2055 aKeyHash |= sCollisionBit;
2056 }
2057
2058 slot.setLive(aKeyHash, std::forward<Args>(aArgs)...);
2059 mEntryCount++;
2060#ifdef DEBUG1
2061 mMutationCount++;
2062#endif
2063 }
2064
2065 public:
2066 void clear() {
2067 forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.clear(); });
2068 mRemovedCount = 0;
2069 mEntryCount = 0;
2070#ifdef DEBUG1
2071 mMutationCount++;
2072#endif
2073 }
2074
2075 // Minimise the memory used. If there are no entries the table is freed,
2076 // otherwise the table is resized to the smallest capacity that doesn't
2077 // overload the table and that is at least sMinCapacity entries.
2078 //
2079 // Since we shrink the table after every remove, you only need to call this if
2080 // you want to free the table when it's empty.
2081 void compact() {
2082 if (empty()) {
2083 // Free the entry storage.
2084 freeTable(*this, mTable, capacity());
2085 incrementGeneration();
2086 setHashShift(
2087 hashShiftForLength(0)); // gives minimum capacity on regrowth
2088 mTable = nullptr;
2089 mRemovedCount = 0;
2090 return;
2091 }
2092
2093 shrinkToBestCapacity();
2094 }
2095
2096 void shrinkToBestCapacity() {
2097 uint32_t bestCapacity = this->bestCapacity(mEntryCount);
2098 if (bestCapacity < capacity()) {
2099 (void)changeTableSize(bestCapacity, DontReportFailure);
2100 }
2101 }
2102
2103 void clearAndCompact() {
2104 clear();
2105 compact();
2106 }
2107
2108 [[nodiscard]] bool reserve(uint32_t aLen) {
2109 if (aLen == 0) {
2110 return true;
2111 }
2112
2113 if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) {
2114 this->reportAllocOverflow();
2115 return false;
2116 }
2117
2118 uint32_t bestCapacity = this->bestCapacity(aLen);
2119 if (bestCapacity <= capacity()) {
2120 return true; // Capacity is already sufficient.
2121 }
2122
2123 RebuildStatus status = changeTableSize(bestCapacity, ReportFailure);
2124 MOZ_ASSERT(status != NotOverloaded)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(status != NotOverloaded)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(status != NotOverloaded))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("status != NotOverloaded"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2124); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded"
")"); do { MOZ_CrashSequence(__null, 2124); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2125 return status != RehashFailed;
2126 }
2127
2128 Iterator iter() const { return Iterator(*this); }
2129
2130 ModIterator modIter() { return ModIterator(*this); }
2131
2132 bool empty() const { return mEntryCount == 0; }
2133
2134 uint32_t count() const { return mEntryCount; }
2135
2136 uint32_t rawCapacity() const { return 1u << (kHashNumberBits - hashShift()); }
2137
2138 uint32_t capacity() const { return mTable ? rawCapacity() : 0; }
2139
2140 Generation generation() const { return Generation(gen()); }
2141
2142 size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
2143 return aMallocSizeOf(mTable);
2144 }
2145
2146 size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
2147 return aMallocSizeOf(this) + shallowSizeOfExcludingThis(aMallocSizeOf);
2148 }
2149
2150 MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const {
2151 if (empty()) {
7
Taking true branch
2152 return Ptr();
8
Calling default constructor for 'Ptr'
10
Returning from default constructor for 'Ptr'
2153 }
2154
2155 HashNumber inputHash;
2156 if (!MaybeGetHash<HashPolicy>(aLookup, &inputHash)) {
2157 return Ptr();
2158 }
2159
2160 HashNumber keyHash = prepareHash(inputHash);
2161 return Ptr(lookup<ForNonAdd>(aLookup, keyHash), *this);
2162 }
2163
2164 MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const {
2165 ReentrancyGuard g(*this);
2166 return readonlyThreadsafeLookup(aLookup);
6
Calling 'HashTable::readonlyThreadsafeLookup'
11
Returning from 'HashTable::readonlyThreadsafeLookup'
2167 }
2168
2169 MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) {
2170 ReentrancyGuard g(*this);
2171
2172 HashNumber inputHash;
2173 if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) {
2174 return AddPtr();
2175 }
2176
2177 HashNumber keyHash = prepareHash(inputHash);
2178
2179 if (!mTable) {
2180 return AddPtr(*this, keyHash);
2181 }
2182
2183 // Directly call the constructor in the return statement to avoid
2184 // excess copying when building with Visual Studio 2017.
2185 // See bug 1385181.
2186 return AddPtr(lookup<ForAdd>(aLookup, keyHash), *this, keyHash);
2187 }
2188
2189 template <typename... Args>
2190 [[nodiscard]] bool add(AddPtr& aPtr, Args&&... aArgs) {
2191 ReentrancyGuard g(*this);
2192 MOZ_ASSERT_IF(aPtr.isValid(), mTable)do { if (aPtr.isValid()) { do { static_assert( mozilla::detail
::AssertionConditionType<decltype(mTable)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2192); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")"
); do { MOZ_CrashSequence(__null, 2192); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
2193 MOZ_ASSERT_IF(aPtr.isValid(), aPtr.mTable == this)do { if (aPtr.isValid()) { do { static_assert( mozilla::detail
::AssertionConditionType<decltype(aPtr.mTable == this)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(aPtr.mTable == this))), 0))) { do { } while (false);
MOZ_ReportAssertionFailure("aPtr.mTable == this", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2193); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mTable == this"
")"); do { MOZ_CrashSequence(__null, 2193); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
2194 MOZ_ASSERT(!aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!aPtr.found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aPtr.found()))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("!aPtr.found()",
"/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2194); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aPtr.found()"
")"); do { MOZ_CrashSequence(__null, 2194); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2195 MOZ_ASSERT(!(aPtr.mKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!(aPtr.mKeyHash & sCollisionBit))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!(aPtr.mKeyHash & sCollisionBit
)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!(aPtr.mKeyHash & sCollisionBit)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2195); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aPtr.mKeyHash & sCollisionBit)"
")"); do { MOZ_CrashSequence(__null, 2195); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2196
2197 // Check for error from ensureHash() here.
2198 if (!aPtr.isLive()) {
2199 return false;
2200 }
2201
2202 MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("aPtr.mGeneration == generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2202); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()"
")"); do { MOZ_CrashSequence(__null, 2202); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2203#ifdef DEBUG1
2204 MOZ_ASSERT(aPtr.mMutationCount == mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPtr.mMutationCount == mMutationCount)>::isValid,
"invalid assertion condition"); if ((__builtin_expect(!!(!(!
!(aPtr.mMutationCount == mMutationCount))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aPtr.mMutationCount == mMutationCount"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2204); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mMutationCount == mMutationCount"
")"); do { MOZ_CrashSequence(__null, 2204); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2205#endif
2206
2207 if (!aPtr.isValid()) {
2208 MOZ_ASSERT(!mTable && mEntryCount == 0)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mTable && mEntryCount == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!mTable && mEntryCount
== 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!mTable && mEntryCount == 0", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2208); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mTable && mEntryCount == 0"
")"); do { MOZ_CrashSequence(__null, 2208); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2209 uint32_t newCapacity = rawCapacity();
2210 RebuildStatus status = changeTableSize(newCapacity, ReportFailure);
2211 MOZ_ASSERT(status != NotOverloaded)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(status != NotOverloaded)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(status != NotOverloaded))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("status != NotOverloaded"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2211); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded"
")"); do { MOZ_CrashSequence(__null, 2211); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2212 if (status == RehashFailed) {
2213 return false;
2214 }
2215 aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash);
2216
2217 } else if (aPtr.mSlot.isRemoved()) {
2218 // Changing an entry from removed to live does not affect whether we are
2219 // overloaded and can be handled separately.
2220 if (!this->checkSimulatedOOM()) {
2221 return false;
2222 }
2223 mRemovedCount--;
2224 aPtr.mKeyHash |= sCollisionBit;
2225
2226 } else {
2227 // Preserve the validity of |aPtr.mSlot|.
2228 RebuildStatus status = rehashIfOverloaded();
2229 if (status == RehashFailed) {
2230 return false;
2231 }
2232 if (status == NotOverloaded && !this->checkSimulatedOOM()) {
2233 return false;
2234 }
2235 if (status == Rehashed) {
2236 aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash);
2237 }
2238 }
2239
2240 aPtr.mSlot.setLive(aPtr.mKeyHash, std::forward<Args>(aArgs)...);
2241 mEntryCount++;
2242#ifdef DEBUG1
2243 mMutationCount++;
2244 aPtr.mGeneration = generation();
2245 aPtr.mMutationCount = mMutationCount;
2246#endif
2247 return true;
2248 }
2249
2250 // Note: |aLookup| may reference pieces of arguments in |aArgs|, so this
2251 // function must take care not to use |aLookup| after moving |aArgs|.
2252 template <typename... Args>
2253 void putNewInfallible(const Lookup& aLookup, Args&&... aArgs) {
2254 MOZ_ASSERT(!lookup(aLookup).found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!lookup(aLookup).found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!lookup(aLookup).found()))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("!lookup(aLookup).found()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2254); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()"
")"); do { MOZ_CrashSequence(__null, 2254); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2255 ReentrancyGuard g(*this);
2256 HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup));
2257 putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...);
2258 }
2259
2260 // Note: |aLookup| may alias arguments in |aArgs|, so this function must take
2261 // care not to use |aLookup| after moving |aArgs|.
2262 template <typename... Args>
2263 [[nodiscard]] bool putNew(const Lookup& aLookup, Args&&... aArgs) {
2264 MOZ_ASSERT(!lookup(aLookup).found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!lookup(aLookup).found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!lookup(aLookup).found()))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("!lookup(aLookup).found()"
, "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2264); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()"
")"); do { MOZ_CrashSequence(__null, 2264); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2265 ReentrancyGuard g(*this);
2266 if (!this->checkSimulatedOOM()) {
2267 return false;
2268 }
2269 HashNumber inputHash;
2270 if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) {
2271 return false;
2272 }
2273 HashNumber keyHash = prepareHash(inputHash);
2274 if (rehashIfOverloaded() == RehashFailed) {
2275 return false;
2276 }
2277 putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...);
2278 return true;
2279 }
2280
2281 // Note: |aLookup| may be a reference pieces of arguments in |aArgs|, so this
2282 // function must take care not to use |aLookup| after moving |aArgs|.
2283 template <typename... Args>
2284 [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup,
2285 Args&&... aArgs) {
2286 // Check for error from ensureHash() here.
2287 if (!aPtr.isLive()) {
2288 return false;
2289 }
2290#ifdef DEBUG1
2291 aPtr.mGeneration = generation();
2292 aPtr.mMutationCount = mMutationCount;
2293#endif
2294 if (mTable) {
2295 ReentrancyGuard g(*this);
2296 // Check that aLookup has not been destroyed.
2297 MOZ_ASSERT(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash
)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2297); AnnotateMozCrashReason("MOZ_ASSERT" "(" "prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash"
")"); do { MOZ_CrashSequence(__null, 2297); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2298 aPtr.mSlot = lookup<ForAdd>(aLookup, aPtr.mKeyHash);
2299 if (aPtr.found()) {
2300 return true;
2301 }
2302 } else {
2303 // Clear aPtr so it's invalid; add() will allocate storage and redo the
2304 // lookup.
2305 aPtr.mSlot = Slot(nullptr, nullptr);
2306 }
2307 return add(aPtr, std::forward<Args>(aArgs)...);
2308 }
2309
2310 void remove(Ptr aPtr) {
2311 MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mTable)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2311); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")"
); do { MOZ_CrashSequence(__null, 2311); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2312 ReentrancyGuard g(*this);
2313 MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPtr.found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2313); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()"
")"); do { MOZ_CrashSequence(__null, 2313); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2314 MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("aPtr.mGeneration == generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2314); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()"
")"); do { MOZ_CrashSequence(__null, 2314); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2315 remove(aPtr.mSlot);
2316 shrinkIfUnderloaded();
2317 }
2318
2319 template <typename KeyInput>
2320 void rekeyWithoutRehash(Ptr aPtr, const Lookup& aLookup, KeyInput&& aKey) {
2321 MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mTable)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2321); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")"
); do { MOZ_CrashSequence(__null, 2321); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2322 ReentrancyGuard g(*this);
2323 MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPtr.found())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2323); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()"
")"); do { MOZ_CrashSequence(__null, 2323); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2324 MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("aPtr.mGeneration == generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h"
, 2324); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()"
")"); do { MOZ_CrashSequence(__null, 2324); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2325 typename HashTableEntry<T>::NonConstT t(std::move(*aPtr));
2326 HashPolicy::setKey(t, std::forward<KeyInput>(aKey));
2327 remove(aPtr.mSlot);
2328 HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup));
2329 putNewInfallibleInternal(keyHash, std::move(t));
2330 }
2331
2332 template <typename KeyInput>
2333 void rekeyAndMaybeRehash(Ptr aPtr, const Lookup& aLookup, KeyInput&& aKey) {
2334 rekeyWithoutRehash(aPtr, aLookup, std::forward<KeyInput>(aKey));
2335 infallibleRehashIfOverloaded();
2336 }
2337
2338 static size_t offsetOfHashShift() {
2339 static_assert(sHashShiftBits == 8,
2340 "callers assume hash shift is stored in a byte");
2341 // The hash shift is stored in the least significant bits of
2342 // mGenAndHashShift. On little-endian platforms, this is the
2343 // same offset as mGenAndHashShift itself. On big-endian platforms,
2344 // we have to add an additional offset to point to the last byte.
2345 // (Or we would if we had JIT support for any big-endian platforms.)
2346 if constexpr (std::endian::native == std::endian::big) {
2347 return offsetof(HashTable, mGenAndHashShift)__builtin_offsetof(HashTable, mGenAndHashShift) + sizeof(mGenAndHashShift) -
2348 sizeof(uint8_t);
2349 } else {
2350 return offsetof(HashTable, mGenAndHashShift)__builtin_offsetof(HashTable, mGenAndHashShift);
2351 }
2352 }
2353 static size_t offsetOfTable() { return offsetof(HashTable, mTable)__builtin_offsetof(HashTable, mTable); }
2354 static size_t offsetOfEntryCount() {
2355 return offsetof(HashTable, mEntryCount)__builtin_offsetof(HashTable, mEntryCount);
2356 }
2357};
2358
2359} // namespace detail
2360} // namespace mozilla
2361
2362#endif /* mozilla_HashTable_h */